Resource identified generating responses characteristics

Resource identified generating responses characteristics

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request “accept” headers.

Answer: If you are getting above exception when rerunning response from spring MVC controller means you are using annotation based response. Below are points:

  • If your controller method is annotate with @ResponseBody then Spring transforms returned data type into JSON format
  • @ResponseBody automatically encodes object in appropriate formats based on accept header of request and presence of JSON or XML libraries in classpath. For below example method in controller will throw above exception:
@RequestMapping(value = "/returnListInResponse.web", method = RequestMethod.GET)
    public @ResponseBody
    List<String> returnListInResponse(@RequestParam("name") String query) {
    
    List<String> list = new ArrayList<String>();
    
    for (int i = 0; i < 100; i++) {
        
        list.add("Java");
        list.add("Honk");
        list.add("Test");
        
    }
    
    return list;
    }
  • Please note above I am using @ResponseBody and returning data type is List. To fix above error we need two things:

1. Annotation driven tag in dispatcher-servlet.xml (Spring context configuration) as shown below:

<mvc:annotation-driven />

2. To encode data in appropriate format you will have to include JSON parser in your class path which will convert return data in appropriate format. For example I am using Jackson parser below is maven dependency that needs to be included in you pom.xml file and if you are using simple eclipse project  then include jackson-mapper-aslxxx.jar in your class path.

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

This should fix the issue Resource identified generating responses characteristics

Leave a Reply

Your email address will not be published. Required fields are marked *