Error HTTP Status 406: @ResponseBody not returning data
Solution 1
I faced this error and when I removed .html
suffix which was mistakenly added to the request URL, this error got resolved!
Solution 2
Try to add "jackson" dependency to your pom.xml (or add appropriate jar in case if you don't use maven).
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.1</version>
</dependency>
Without this lib you can return only String or similar to String standard types
Solution 3
I have the same problem, but I figure out :
(1) remove
headers={"Accept: application/json, text/javascript"}
(2) add this into your pom.xml :
<!-- add jackson to support restful API, otherwise the API will return 406 error -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
(3) change produces:
produces={"application/json"}
(4) remove content-negotiation-manager if you have
Solution 4
You should define the types that can be produced via the produces
attribute of the @RequestMapping
annotation, and not through setting custom headers.
@RequestMapping(value="/movieTheater", method=RequestMethod.GET, produces={"application/json","application/xml"})
public @ResponseBody ResponseText getCustomerInput(@RequestParam("name") String name, @RequestParam("price") Double price) {
// ...
}
Note that you probably ought to set only concrete types in the produces
attribute, saying what types can actually be produced; claiming to produce anything isn't actually all that useful unless you're serving up files and doing real work to determine MIME types. Serializing as JSON and XML are very common options, but serializing as a video stream is… less common, shall we say?
You need appropriate message converters as well.
Related videos on Youtube
kajarigd
Updated on September 15, 2022Comments
-
kajarigd over 1 year
I have the following code in REST, Spring MVC. This code is supposed to return a JSON type data structure called ResponseText:
@RequestMapping(value="/movieTheater", headers = {"ACCEPT=*/*"}, method=RequestMethod.GET) public @ResponseBody ResponseText getCustomerInput(@RequestParam("name") String name, @RequestParam("price") Double price) { Transaction transaction = new Transaction(); ResponseText result = new ResponseText(); transaction.setMovieName(name); transaction.setTicketPrice(price); transaction.setDatetime(new Date()); if(transactionService.addTransaction(transaction)) result.setMessage(ResponseStatus.SUCCESS.getStatus()); else result.setMessage(ResponseStatus.FAILED.getStatus()); return result; }
But when I am executing this code via the below URL in the browser, I am getting the below error:
URL:
http://localhost:8080/SpringMVCMerchant/movieTheater.htm?name=Smurfs&price=300.00
Error:
HTTP Status 406 - type Status report message description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
I am unable to identify what I am doing wrong here. I looked up on the Net explaining this error, but still don't know what I am missing. I have given ACCEPT="/", which is supposed to cover all sorts of responses, right? Please help! Thanks in advance!
** When I added the header
headers={"Accept: application/json, text/javascript"}
instead of the above one, I got the following error:
HTTP Status 405 - Request method 'GET' not supported
-
kajarigd over 10 yearsThanks for your reply. I have this jar already. Still it is not working. Do I need to add anything to my applicationcontext.xml?
-
yname over 10 yearsNo, no special configuration in applicationcontext.xml needed
-
yname over 10 yearsTry remove all attributes from @RequestMapping except of "value" and "method"
-
kajarigd over 10 yearsThanks for your reply. But when I am adding produces, I am getting the following error: The attribute produces is undefined for the annotation type RequestMapping. Any idea?
-
kajarigd over 10 yearsPlease see the above addition I made to my question. A new observation.
-
kajarigd over 10 yearsPlease see the above addition I made to my question. A new observation.
-
kajarigd over 10 yearsI updated the spring jars and now produces got accepted fine. But when I am adding produces, I am getting the error: HTTP Status 404 - /SpringMVCMerchant/movieTheater.htm, the requested resource is not available.
-
Norbert van Nobelen over 8 yearsOr the other way around: Remove the
.html
to get rid of the error (really depends on the Spring http configuration. (+1 on your answer)