1

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

LuckyProgrammer
  • 79
  • 1
  • 1
  • 8

2 Answers2

6

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.

500 Server error
  • 644
  • 13
  • 28
  • provide the CURL call to the rest service. you can generate it from Postman. – 500 Server error Jan 30 '17 at 09:15
  • how can I do this? – LuckyProgrammer Jan 30 '17 at 09:19
  • 2
    @YanaGlance You are still doing a `PUT` on the `/clients` endpoint, but you haven't defined any method with `@PUT` to listen on that endpoint (you have one for `/clients/{id}`). Change the `PUT` in postman to be `POST`. – Mike Jan 30 '17 at 10:07
  • I saw error: "method not allowed" even when I typed this path: localhost:8080/goAway/rest/clients/{id}. And also I found out that my server allows only POST,GET,OPTIONS. I don't know how to change this configuration. – LuckyProgrammer Jan 30 '17 at 14:25
  • What application server are you using? – 500 Server error Jan 30 '17 at 14:29
  • 1
    @YanaGlance did you actually include `{id}` in the URL? That won't work, you need to replace it with an actual valid id number – Mike Jan 31 '17 at 11:40
0

I think the problem is not in your postman, it's in the server side. Check this post, it solved the issue for me, in the end my config looks like this (green parts is the only thing you care here)

ASP.NET Web API - PUT & DELETE Verbs Not Allowed - IIS 8

enter image description here

Yogurtu
  • 2,656
  • 3
  • 23
  • 23