406 HTTP status error: @ResponseBody does not return data

I have the following code in REST, Spring MVC. This code should return a JSON 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 execute this code through the below URL in the browser, I get the following 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 cannot determine what I am doing wrong here. I looked on the web explaining this error, but still do not know what I am missing. I gave ACCEPT = "/" which should cover all kinds of answers, right? Please help! Thanks in advance!

** When I added the title

 headers={"Accept: application/json, text/javascript"} 

instead of the above, I got the following error:

 HTTP Status 405 - Request method 'GET' not supported 
+4
source share
6 answers

I ran into this error, and when I removed the .html suffix that was mistakenly added to the request URL, this error was resolved!

+3
source

Try adding the jackson dependency to your pom.xml (or add the appropriate jar if you are not using maven).

 <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.7.1</version> </dependency> 

Without this lib, you can only return String or similar standard String types

+2
source

You must define the types that can be created using the produces attribute of @RequestMapping annotations, and not by 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 should probably only specify specific types in the produces attribute, saying which types can actually be created; an application for something is not really useful if you are not servicing files and are not doing the real job of defining MIME types. Serialization as JSON and XML are very common options, but serialization as a video stream ... less common, let's say?

You also need appropriate message converters .

+1
source

I have the same problem, but I find out:

(1) remove

 headers={"Accept: application/json, text/javascript"} 

(2) add this to 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) the change produces:

 produces={"application/json"} 

(4) remove the content negotiation manager, if you have

+1
source

Did you forget to implement Serializable in ResponseText?

0
source

For me, the problem was that I turned on the display of context annotation:

 <context:annotation-config/> 

But forgot to enable mvc annotation driven:

 <mvc:annotation-driven/> 

For some reason, in this case, Spring returns 406 instead of 404 or is relevant. I do not know how to do that.

What is the difference between <mvc: annotation-driven /> and <context: annotation-config /> in a servlet?

0
source

Source: https://habr.com/ru/post/1496424/


All Articles