Eswar
Eswar

Reputation: 1212

405 response code on POST to my java api from POSTMAN

I have created a simple java restAPi in eclipse as dynamic web project. I am attempting to POST from the POSTMAN to the same endpoint(that I am doing GET) but I get 405 response code.

The web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>JsonJavaApi</display-name>

<servlet>
  <servlet-name>Book API</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>test</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>Book API</servlet-name>
   <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

The Book class:

public class Book {

public String author;
public String name;

public Book(String author, String name) {
    super();
    this.author=author;
    this.name=name;
}
public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Book() {
    super();
}

}

The restapi code:

@Path("/jason")
public class JavajsonRestApi {
@GET
@Path("/json")
@Produces(MediaType.APPLICATION_JSON)
public Book getJson() {

    Book book = new Book();
    book.setAuthor("eswar");
    book.setName("eswar");
    return book;
}
}

The endpoint :

http://localhost:8080/JsonJavaApi/rest/jason/json

As I understand it, the permission to post is not given to this end point but how to do I give it such that the client side can post from the body itself, this helps get rid of the 405 error?

Any help is appreciated.

Upvotes: 1

Views: 2267

Answers (3)

Louis-wht
Louis-wht

Reputation: 555

HTTP 405 Status codes means the method is not authorized but the url itself exists and points to a resource. If you want to access your endpoint using POST method, you will have to either create another endpoint with @POST annotation on top of it, add @POST annotation on top of your actual endpoint (not sure this one works, never tried so far), or simply change @GET to @POST.

EDIT

I think you've got some misunderstanding of what and how HTTP works. You are making an HTTP request. This request might have a body (GET and DELETE methods don't have bodies). The body can be gathered in Spring using @RequestBody SomePojoClass body as one of the parameters of your controller method. Please see : https://baeldung.com/spring-request-response-body

Upvotes: 2

YK S
YK S

Reputation: 3440

HTTP 405 status code means Method Not Allowed. Since you have the method annotated with @GET and you are trying to do the @POST so you are getting the status code.

So if you want to make a POST request to same endpoint then remove the @GET annotation as you can have a method annotated with only one HTTP annotations at a time.

Upvotes: 0

Raja Ramachandran
Raja Ramachandran

Reputation: 456

Add web.xml

   <servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer    
    </servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.sample.rest</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

And update jersey version 1.8 above

Upvotes: 0

Related Questions