Java 意外'<';在执行REST请求时?

Java 意外'<';在执行REST请求时?,java,rest,jpa,jpql,Java,Rest,Jpa,Jpql,我最近一直在潜心研究Java Rest。然而,每次我试图通过邮递员做一些事情,并提出GET请求,结果都是一样的 我的其余部分是这样写的: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor.

我最近一直在潜心研究Java Rest。然而,每次我试图通过邮递员做一些事情,并提出GET请求,结果都是一样的

我的其余部分是这样写的:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rest;

import DBMapper.DBMapper;
import com.google.gson.Gson;
import entity.Country;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * REST Web Service
 *
 * @author William Pfaffe
 */
@Path("world")
public class WorldResource {
    private EntityManager em;
    private EntityManagerFactory emf;
    private DBMapper db = new DBMapper(em, emf);
    private List<Country> list;
    private final  Gson gson = new com.google.gson.GsonBuilder().setPrettyPrinting().create();

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of WorldResource
     */
    public WorldResource() {

    }

    /**
     * Retrieves representation of an instance of rest.WorldResource
     * @return an instance of java.lang.String
     */
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public String getXml() {
        //TODO return proper representation object
        throw new UnsupportedOperationException();
    }

    @GET
    @Path("/countries")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getCountries() {
        list = db.getAllCountries();
        String jsonEx = gson.toJson(list, List.class);
        return Response.status(200).entity(jsonEx).build();
    }

    /**
     * PUT method for updating or creating an instance of WorldResource
     * @param content representation for the resource
     */
    @PUT
    @Consumes(MediaType.APPLICATION_XML)
    public void putXml(String content) {
    }
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包裹架;
导入DBMapper.DBMapper;
导入com.google.gson.gson;
进口实体。国家;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入javax.persistence.EntityManager;
导入javax.persistence.EntityManagerFactory;
导入javax.ws.rs.core.Context;
导入javax.ws.rs.core.UriInfo;
导入javax.ws.rs.Consumes;
导入javax.ws.rs.products;
导入javax.ws.rs.GET;
导入javax.ws.rs.Path;
导入javax.ws.rs.PUT;
导入javax.ws.rs.PathParam;
导入javax.ws.rs.core.MediaType;
导入javax.ws.rs.core.Response;
/**
*RESTWeb服务
*
*@作者威廉·普法夫
*/
@路径(“世界”)
公共类世界资源{
私人实体管理者;
私人实体管理工厂emf;
私有DBMapper db=新DBMapper(em,emf);
私人名单;
private final Gson Gson=new com.google.Gson.GsonBuilder().setPrettyPrinting().create();
@上下文
私有信息上下文;
/**
*创建WorldResource的新实例
*/
公共世界资源(){
}
/**
*检索rest.WorldResource实例的表示形式
*@返回java.lang.String的实例
*/
@得到
@生成(MediaType.APPLICATION\u XML)
公共字符串getXml(){
//TODO返回正确的表示对象
抛出新的UnsupportedOperationException();
}
@得到
@路径(“/国家”)
@产生(MediaType.APPLICATION_JSON)
公众回应(国家){
list=db.getAllCountries();
字符串jsonEx=gson.toJson(list,list.class);
返回Response.status(200).entity(jsonEx.build();
}
/**
*更新或创建WorldResource实例的PUT方法
*@param资源的内容表示形式
*/
@放
@使用(MediaType.APPLICATION_XML)
公共void putXml(字符串内容){
}
}
我的应用程序配置如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rest;

import java.util.Set;
import javax.ws.rs.core.Application;

/**
 *
 * @author William Pfaffe
 */
@javax.ws.rs.ApplicationPath("api")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        addRestResourceClasses(resources);
        return resources;
    }

    /**
     * Do not modify addRestResourceClasses() method.
     * It is automatically populated with
     * all resources defined in the project.
     * If required, comment out calling this method in getClasses().
     */
    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(rest.WorldResource.class);
    }

}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包裹架;
导入java.util.Set;
导入javax.ws.rs.core.Application;
/**
*
*@作者威廉·普法夫
*/
@javax.ws.rs.ApplicationPath(“api”)
公共类应用程序配置扩展应用程序{
@凌驾
public Set>resources=new java.util.HashSet();
添加资源类(资源);
归还资源;
}
/**
*不要修改addRestResourceClasses()方法。
*它会自动填充
*项目中定义的所有资源。
*如果需要,在getClasses()中调用此方法时注释掉。
*/

私有void addrestresourcelasses(设置你是否没有注意到
状态:404未找到
?“结果”是什么?你期望发生什么,实际发生了什么,区别是什么,等等检查
原始
选项卡错误截图中唯一重要的是404。在得到修复之前,忽略所有其他内容。你将部署到哪个应用服务器,以及什么版本?你有web.xml吗?你也能分享一下吗(如果不是空白的话)
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DBMapper;

import entity.Country;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author William Pfaffe
 */
public class DBMapper {

    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu");
    private EntityManager em = emf.createEntityManager();

    public List<Country> getAllCountries() {
        Query query = em.createQuery("SELECT c from Country c");
        List<Country> list = query.getResultList();
        return list;
    }

    public List<Country> getCountryGreaterThanPopulation(int population) {
        Query query = em.createQuery("SELECT c from Country c where c.population > :popu");
        query.setParameter("popu", population);
        List<Country> list = query.getResultList();
        return list;
    }

    public List<Country> getCountryByCountryCode(String cc) {
        Query query = em.createQuery("SELECT c from Country c where c.code = :cc");
        query.setParameter("cc", cc);
        List<Country> list = query.getResultList();
        return list;
    }

    public Country getDenmark() {
        Query query = em.createQuery("SELECT c from Country c where c.code = :cc");
        query.setParameter("cc", "DNK");
        Country list = (Country) query.getSingleResult();
        return list;
    }

}