4

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
  • 1,299
  • 3
  • 28
  • 46

6 Answers6

3

I faced this error and when I removed .html suffix which was mistakenly added to the request URL, this error got resolved!

James Jithin
  • 10,183
  • 5
  • 36
  • 51
  • Or the other way around: Remove the `.html` to get rid of the error (really depends on the Spring http configuration. (+1 on your answer) – Norbert Nov 18 '15 at 16:25
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

yname
  • 2,189
  • 13
  • 23
1

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.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Thanks 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 Aug 11 '13 at 18:35
  • Please see the above addition I made to my question. A new observation. – kajarigd Aug 11 '13 at 18:51
  • I 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. – kajarigd Aug 11 '13 at 19:26
1

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

Justin
  • 1,050
  • 11
  • 26
0

Did you maybe forget to implement Serializable in the ResponseText?

ruben056
  • 112
  • 8
0

For me the problem was that I included context annotation driving:

<context:annotation-config/>

But forgot to include mvc annotation driven:

<mvc:annotation-driven/> 

For some reason in this case Spring returns 406 instead of 404 or relevant. I don't know way.

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

Community
  • 1
  • 1
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192