Guice servlet 405 err: seems to not set Content-type set to “text/json” for MediaType.APPLICATION_JSON

StackOverflow https://stackoverflow.com/questions/7041578

Question

I'm making a call from jQGrid to a Guice servlet that has the following binding:

@Produces({MediaType.APPLICATION_JSON})
@Path("/{param}")
public String getJson(@PathParam("param") String param) {
    ...
    return return json.toString();
}

Requesting the url directly, I can see the JSON. When jqgrid executes the request, I get 405 method not allowed response. I've seen this happen before when the returning page doesn't have the Content-type set to "text/json" (jqgrid is not very flexible here).

HERE IS THE REQUEST: Key Value Request POST /myapp/json/jqgrid/json ... HTTP/1.1 x-requested-with XMLHttpRequest Accept-Language en-us Referer http://localhost:8080/myapp/myPage... Accept application/json, text/javascript, / Content-Type application/x-www-form-urlencoded Accept-Encoding gzip, deflate User-Agent Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0) Host localhost:8080 Content-Length 63 Connection Keep-Alive Cache-Control no-cache

HERE IS THE RESPONSE: Key Value Response HTTP/1.1 405 Method Not Allowed Server Apache-Coyote/1.1 Allow GET,OPTIONS,HEAD Content-Type text/html;charset=utf-8 Content-Length 1034

Any thoughts on how to get the guice servlet to set the Content-type to "text/json" and allow the response?

Was it helpful?

Solution

This one is solved. I was using a @GET annotation and jQGrid was issuing a post. I changed the @POST and it started working. This may solve the problem for others with related 405 errors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top