PrecisionPete
PrecisionPete

Reputation: 3401

Jersey REST resource returning HTTP Status 405 - Method Not Allowed

I am trying to build a simple REST JSON api using Tomcat 7 and Jersey 2. The GETs all work great. But the POSTs/PUTs return the dreaded 405.

I know this is a popular question. But I have researched all the other answers and they do not help in my case.

I am running it on my local Linux workstation. I am using real Oracle JDK, Eclipse Mars, Tomcat 7, and Maven to handle the dependencies.

As part of the same app, I also have a conventional jsp/servlet interface that does CRUD operations just fine. So POST is working for the servlets.

When a POST/PUT fails, there is no record of it connecting in the tomcat log. I am using the POSTMAN app in Chrome to generate the requests.

I've tried all kinds of permutations of things but to no affect. I suspect looking at the code is not going to reveal an easy answer. But it is below.

Can someone please suggest a method to debug this? If it's not failing on the server. Where is it going?

Thanks very much in advance

I'm using Servlet 3 so no need to define it in the web.xml

Everything from the DAO back is working so I'll skip that code...

RestApplication.java

package resources;

import javax.ws.rs.ApplicationPath;

import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath("rest")
public class RestApplication extends ResourceConfig {
    public RestApplication() {
        packages("resources");
    }
}

PersonResource.java

package resources;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringUtils;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;

import dao.PersonDAO;
import model.Person;

@Path("/persons")
public class PersonResource {

    private MongoClientURI cs;
    private MongoClient mc;
    private PersonDAO personDAO;

    public PersonResource() {
        super();

        cs = new MongoClientURI("mongodb://localhost:27017");
        mc = new MongoClient(cs);
        personDAO = new PersonDAO(mc);
    }

    @Path("/")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Person> getPersonsAll() {

        List<Person> persons = personDAO.getAllPerson();
        return persons;
    }

    @Path("/add")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response addPerson(Person person) {

        personDAO.createPerson(person);
        return Response.status(201).build();
    }

    @Path("/get/{id}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Person getPersonById(@PathParam("id") String id) {

        Person person = new Person();
        if (StringUtils.isNotEmpty(id)) {
            person.setId(id);
            person = personDAO.getPerson(person);
        }
        return person;
    }

}

Upvotes: 2

Views: 1361

Answers (1)

PrecisionPete
PrecisionPete

Reputation: 3401

For some reason the error 405 suddenly became error 415 - Unsupported Media Type. Then it was because POSTMAN was not setting the headers correctly.

I then tried the other Chrome App Advanced REST Client... Once I specified a header for application/json everything started working.

I suspect this means it was more a problem with POSTMAN than the code.

Sometimes you have to ask the question on SO in order to find your own answer...

Thanks for listening.

Upvotes: 1

Related Questions