1

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.

Eswar
  • 1,201
  • 19
  • 45

3 Answers3

2

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

Community
  • 1
  • 1
Louis-wht
  • 535
  • 5
  • 17
  • if I change simple change GET to POST will I be able to get?? – Eswar Aug 24 '18 at 11:16
  • Nope, not anymore, so you might look into having both annotations but be careful that GET requests don't have an HTTP body so mapping the same method to both GET and POST is absolutely not recommended, this does not respect RESTful API protocol and might lead to further problems... – Louis-wht Aug 24 '18 at 11:17
  • This is likely to be a pattern problem so you should probably rethink the way you want to access your endpoint. Further more, GET method is to get information/objects from the server side without altering its state which is what you are doing here so I recommend you only use `GET` method – Louis-wht Aug 24 '18 at 11:19
  • Replacing GET with POST or using both POST and GET and POST together doesn't work. I have done like : @POST@Path("/json") @Produces(MediaType.APPLICATION_JSON)public Book postJson(@Context Book book) {return book;}.........But this is not reflecting the post made from the body of the postman...I know I can use query params but I want to do it this way....So how? – Eswar Aug 24 '18 at 11:21
  • You might want to look some more information about what is REST here : https://stackoverflow.com/questions/4663927/what-is-rest-slightly-confused – Louis-wht Aug 24 '18 at 11:22
  • Yes but GET requests don't have bodies so you would have to create two different controllers then... And as stated earlier, this will probably lead to some further pattern problems as it's most likely not recommended – Louis-wht Aug 24 '18 at 11:22
  • say I have changed the end point: but how to I capture the POST made from the body of the POSTMAN......And I can all access premissions to an endpoint such as GET/POST/PUT/OPTIONS....On many tutorials, the end points have such permissions where we can PUT/POST/GET from/to the same endpoint... – Eswar Aug 24 '18 at 11:30
  • Well I've never heard of this kind of endpoints so far and it's probably not a good idea though... Sorry but I cannot help you further with this... – Louis-wht Aug 24 '18 at 11:32
  • Can you tell me how to capture the POST made to a different endpoint from the body part of the POSTMAN? – Eswar Aug 24 '18 at 11:37
  • I think you've got some misunderstanding of what and how HTTP works. You are making an HTTP request (and we honestly don't care wether it is POSTMAN or your browser because it's still 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://www.baeldung.com/spring-request-response-body – Louis-wht Aug 24 '18 at 11:40
  • I will add this to my answer, if it answer your original question, please mark it as an accepted answer. – Louis-wht Aug 24 '18 at 11:41
0

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

0

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.

Yug Singh
  • 3,112
  • 5
  • 27
  • 52