我尝试在我的网络项目上使用 REST。POST 有效,但 DELETE 和 PUT 不起作用,我会看到错误:HTTP 状态 405 - 不允许方法。并且 GET 根本不起作用:
“‘id’:RFC 2068 中未定义,且 Servlet API 不支持。描述:服务器不支持满足此请求所需的功能。”
这是我的代码:
package rest;
import domain.model.Client;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.*;
import javax.xml.bind.annotation.XmlRootElement;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;
@XmlRootElement
@Path("/clients")
@Stateless
public class ClientResources {
@PersistenceContext
EntityManager entityManager;
@GET
@Consumes(MediaType.APPLICATION_JSON)
public Response getAll() {
List<Client> matchHistories = new ArrayList<>();
for (Client m : entityManager
.createNamedQuery("client.all", Client.class)
.getResultList())
matchHistories.add(m);
return Response.ok(new GenericEntity<List<Client>>(matchHistories) {
}).build();
} …Run Code Online (Sandbox Code Playgroud)