REST - HTTP Status 405 - Method Not Allowed

2019-03-05 06:42发布

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

1条回答
Juvenile、少年°
2楼-- · 2019-03-05 07:32

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.

Postman

To get the HTTP/CURL/... call generated by Postman follow this image.

Generated Code

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.

查看更多
登录 后发表回答