REST - HTTP状态405 - 方法不允许 [英] REST - HTTP Status 405 - Method Not Allowed
问题描述
我尝试在我的web项目中使用REST。 POST工作,但DELETE和PUT不起作用,我会看到错误:HTTP状态405 - 方法不允许。并且GET根本不起作用:
& quot; id& quot ;:未在RFC 2068中定义,并且不受Servlet API支持。 :服务器不支持满足此请求所需的功能。
这是我的代码:
包休息;
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
公共类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);
$ b $ return Response.ok(new GenericEntity< List< Client>>(matchHistories){
})。build();
$ b @POST
@Consumes(MediaType.APPLICATION_JSON)
public Response Add(Client client){
entityManager.persist客户);
return Response.ok(client.getId())。build();
$ b @PUT
@Path(/ {id})
@Consumes(MediaType.APPLICATION_JSON)
public update update(@PathParam (id)int id,Client p){
Client result = entityManager.createNamedQuery(client.id,Client.class)
.setParameter(clientId,id)
.getSingleResult();
if(result == null){
return Response.status(404).build();
}
result.setName(p.getName());
result.setSurname(p.getSurname());
entityManager.persist(result);
return Response.ok()。build();
}
@GET
@Path(/ {id})
@Produces(MediaType.APPLICATION_JSON)
public Response get(@PathParam (id)int id){
Client result = entityManager.createNamedQuery(client.id,Client.class)
.setParameter(clientId,id)
.getSingleResult );
if(result == null){
return Response.status(404).build();
}
return Response.ok(result).build();
@DELETE
@Path(/ {id})
public response delete(@PathParam(id)int id){
Client result = entityManager.createNamedQuery(client.id,Client.class)
.setParameter(clientId,id)
.getSingleResult();
if(result == null)
return Response.status(404).build();
entityManager.remove(result);
return Response.ok()。build();
}
}
在Postman中,我写道:
{
id:1,
name: Adam
取得HTTP / CURL /。 ..由邮差生成的电话跟着这张图片。
当Mike在注释中注意到您的PUT服务时,从路径 / clients / {id}
到达。所以你必须使用客户端的ID来调用它,以便PUT正常工作。
HTTP方法POST和PUT不是CRUD的
的HTTP等价物创建并更新。他们都有不同的目的。在某些情况下,使用PUT到
创建资源,或者使用POST来更新资源是相当可能的,有效的,甚至是首选。
使用当你可以通过一个特定的
资源完全更新一个资源时。例如,如果您知道某篇文章驻留在
http://example.org/article/1234 ,您可以直接通过PUT在此URL上放置本文的新资源
。
如果您不知道实际资源位置,例如,当
添加一篇新文章,但不知道在哪里存储它时,
可以将它张贴到一个URL,并让服务器决定实际的URL。 p>
I try to use REST on my web-project. POST works, but DELETE and PUT don't work, I will see the error: HTTP Status 405 - Method Not Allowed. And GET doesn't work at all:
""id": is not defined in RFC 2068 and is not supported by the Servlet API. description: The server does not support the functionality needed to fulfill this request."
This is my code:
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();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response Add(Client client) {
entityManager.persist(client);
return Response.ok(client.getId()).build();
}
@PUT
@Path("/{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response update(@PathParam("id") int id, Client p) {
Client result = entityManager.createNamedQuery("client.id", Client.class)
.setParameter("clientId", id)
.getSingleResult();
if (result == null) {
return Response.status(404).build();
}
result.setName(p.getName());
result.setSurname(p.getSurname());
entityManager.persist(result);
return Response.ok().build();
}
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response get(@PathParam("id") int id) {
Client result = entityManager.createNamedQuery("client.id", Client.class)
.setParameter("clientId", id)
.getSingleResult();
if (result == null) {
return Response.status(404).build();
}
return Response.ok(result).build();
}
@DELETE
@Path("/{id}")
public Response delete(@PathParam("id") int id) {
Client result = entityManager.createNamedQuery("client.id", Client.class)
.setParameter("clientId", id)
.getSingleResult();
if (result == null)
return Response.status(404).build();
entityManager.remove(result);
return Response.ok().build();
}
}
In Postman I wrote this:
{
"id" : 1,
"name" : "Adam"
}
enter image description here enter image description here
Check your postman. You should have it set up as the image below. If your body type isn't application/json
or your method isn't POST
you'll get the Method not allowed error.
To get the HTTP/CURL/... call generated by Postman follow this image.
Your PUT service is reached from the path /clients/{id}
as Mike noticed in the comments. So you'll have to call it with the ID of a client for PUT to work.
The HTTP methods POST and PUT aren't the HTTP equivalent of the CRUD's create and update. They both serve a different purpose. It's quite possible, valid and even preferred in some occasions, to use PUT to create resources, or use POST to update resources.
Use PUT when you can update a resource completely through a specific resource. For instance, if you know that an article resides at http://example.org/article/1234, you can PUT a new resource representation of this article directly through a PUT on this URL.
If you do not know the actual resource location, for instance, when you add a new article, but do not have any idea where to store it, you can POST it to an URL, and let the server decide the actual URL.
这篇关于REST - HTTP状态405 - 方法不允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!