Why am i getting 405 Method Not Allowed while doing a POST Request
(1) dataType: 'jsonp'
should be json
. Get rid of all the the jsonp
related stuff. It should be json. jsonp will get automatically changed to a GET request, hence the 405 (method not allowed). Your resource method accepts POST.
(2) Problem with CORS. You need a filter to handle the pre-flight. If you are using Jersey 1, you can use this class
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;
public class CORSFilter implements ContainerResponseFilter {
@Override
public ContainerResponse filter(ContainerRequest request,
ContainerResponse response) {
response.getHttpHeaders().add("Access-Control-Allow-Origin", "*");
response.getHttpHeaders().add("Access-Control-Allow-Headers",
"origin, content-type, accept, authorization");
response.getHttpHeaders().add("Access-Control-Allow-Credentials", "true");
response.getHttpHeaders().add("Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS, HEAD");
return response;
}
}
Then register it like
resourceConfig.getContainerResponseFilters().add(new CORSFilter());
With web.xml, add this
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.yourpackage.CORSFilter</param-value>
</init-param>
inside the <servlet>
element that contains the Jersey servlet
Pawan
Updated on June 05, 2022Comments
-
Pawan almost 2 years
I am new to the rest services. I am trying to create a service that accepts json string from a client. I am getting 405 error when I am calling this service using JQuery. Below is the Java code for ws:
This is the way i am posting a request to JERSEY POST RESTFUL Webservice .
var orderinfo = {'ordersplitjson': ordersplitjson, 'customer_id': cust_id , 'homedelivery': homedelivery, 'seatnum' :seatnum , 'locationname':location_nam , 'rownum':rownum}; var json_data = JSON.stringify(orderinfo); var ajaxcallquery = $.ajax({ type:'POST', dataType: 'jsonp', data: json_data, contentType: "application/json; charset=utf-8", url:url+'/OMS/oms1/orderinsertservice', jsonpCallback:'jsonCallback', jsonp:false, success: function(response) { }, error: function(jqxhr, status, errorMsg) { alert('Failed! ' + errorMsg); } }); public class OrdersInsertService { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces("application/json") public String getData(OrderInfo order,@Context HttpServletResponse serverResponse) throws JSONException { serverResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD"); serverResponse.addHeader("Access-Control-Allow-Credentials", "true"); serverResponse.addHeader("Access-Control-Allow-Origin", "*"); serverResponse.addHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With"); serverResponse.addHeader("Access-Control-Max-Age", "60"); } } package com.util; public class OrderInfo { String ordersplitjson; public String getOrdersplitjson() { return ordersplitjson; } public void setOrdersplitjson(String ordersplitjson) { this.ordersplitjson = ordersplitjson; } public String getCustomer_id() { return customer_id; } public void setCustomer_id(String customer_id) { this.customer_id = customer_id; } public String getHomedelivery() { return homedelivery; } public void setHomedelivery(String homedelivery) { this.homedelivery = homedelivery; } public String getSeatnum() { return seatnum; } public void setSeatnum(String seatnum) { this.seatnum = seatnum; } public String getLocationname() { return locationname; } public void setLocationname(String locationname) { this.locationname = locationname; } public String getRownum() { return rownum; } public void setRownum(String rownum) { this.rownum = rownum; } String customer_id; String homedelivery; String seatnum; String locationname; String rownum; }
Could anybody please let me know how to fix this
I am using Jersey 1 ,When i used your class its giving me a compiltion error in eclipse as hwon in picture
-
Pawan over 9 yearsI just replaced jsonp to json ,i am getting No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
-
Paul Samsotha over 9 yearsGet rid of everything jsonp related. jsonp will automatically made to GET (hence the 405). jsonp with POST is no good.
-
Pawan over 9 yearsWhen i modified jsonp to json No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
-
Pawan over 9 yearsI even modifed my code to public String getData(OrderInfo order,@Context HttpServletResponse serverResponse) throws JSONException { serverResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD"); serverResponse.addHeader("Access-Control-Allow-Credentials", "true"); serverResponse.addHeader("Access-Control-Allow-Origin", ""); serverResponse.addHeader("Access-Control-Allow-Headers", ""); serverResponse.addHeader("Access-Control-Max-Age", "60");
-
Paul Samsotha over 9 years405 means GET is not allowed. That was the original problem. Now you seem to have a cors problem. First check if the method is even getting hit. Use a print statement. If not, then try and use a cors response fitler. See here for the filter
-
Pawan over 9 yearsI have seen the method isn't being hit .
-
Paul Samsotha over 9 yearsThen use a container response filter. The question on the post is using Jersey 1, and my answer uses Jersey 2.
-
Pawan over 9 years@peeskillet , even i am uisng jersey 1 rigth now .
-
Paul Samsotha over 9 yearsUse the
CORSFilter
class in the question I linked to. -
Pawan over 9 yearsI have written a new class as you mentioned , i am getting The method filter(ContainerRequest, ContainerResponse) of type CORSFilter must override a superclass method - implements com.sun.jersey.spi.container.ContainerResponseFilter.filter
-
Paul Samsotha over 9 yearsIf you are using Jersey 1, use the class from the OP (original post), not the answer. The answer code is using Jersey 2
-
Paul Samsotha over 9 yearsI'll post an answer. Give me second
-
-
Pawan over 9 yearsHi , I using Jersey 1 and when i used your class i am getting a compilation erorr in eclipse as shon in the picture in my question , could you please let me know how to ged rid of this error .
-
Pawan over 9 yearsSorry for that , its giving me - implements com.sun.jersey.spi.container.ContainerResponseFilter.filter - The method filter(ContainerRequest, ContainerResponse) of type CORSFilter must override a superclass method
-
Paul Samsotha over 9 yearsI don't know I have no problems. I copied and pasted the above code from my IDE
-
Pawan over 9 yearsSorry for that i was using JDK 1.5 , redone to JDK 1.6 the error was gone . Now how do i get access to resourceConfig.getContainerResponseFilters().add(new CORSFilter()); inside my OrderInsertService class ?
-
Paul Samsotha over 9 yearsAre you using web.xml or
ResourceConfig
class for the application? -
Pawan over 9 yearsThank you very much , that cross origin error was gone , now i am getting 400 Bad Request in the Chrome degubber tools network tab
-
Pawan over 9 yearsCan not deserialize instance of java.lang.String out of START_OBJECT token at [Source: org.apache.catalina.connector.CoyoteInputStream@105a248; line: 1, column: 2] (through reference chain: com.util.OrderInfo["ordersplitjson"])
-
Pawan over 9 yearsLet us continue this discussion in chat.
-
Pawan over 9 yearsNow atleaset the service is being hit , thank you very much ,i will figure that issue .