我最近潜入Java Rest。但是,每当我尝试通过Postman做一些事情并发出GET请求时,结果都是一样的。
我的REST是这样写的:
/*
* 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) {
}
}
我的ApplicationConfig是这样的:
/*
* 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);
}
}
我尝试了几乎所有的东西,尝试不同的返回类型,而不是返回响应对象。每次结果都是一样的。
**编辑:问题来自我的ENTITYMANAGER! 我的DBMAPPER的代码显示在下面!
MultiException有2个例外。他们是: 1. java.lang.NoClassDefFoundError:javax / persistence / Persistence 2. java.lang.IllegalStateException:无法执行操作:在rest.TestResource上创建
/*
* 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;
}
}