1

I'm using @ResponseBody to get the json to Browser.I could get the data,but now if I try getting array of Strings or list of Strings then I get the following

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

Why the above status?

The following is the Object I'm returning from controller

public  class Temp
{    
 List<String> strArr=new ArrayList<String>();   
 public Temp()
 {
    strArr.add("1");
    strArr.add("2");
    strArr.add("3");
    strArr.add("4");
    System.out.println("temp="+strArr);
 }
}

The following is the controller

@RequestMapping ( value = "/temp.htm" , method = RequestMethod.GET,produces="application/json")
public @ResponseBody Temp getTemp()
{
        return new Temp();
}

I could get normal String data but if I try to get array or list of String I get the above status.

Please help

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
pavi
  • 654
  • 1
  • 9
  • 29

2 Answers2

1

a) You need to set your accept headers to application/json

b) Your bean class is not a proper Java bean. You need to expose a getter method:

public List<String> getStrArr(){return strAddr;}

c) Do you have Jackson on your classpath, and do you have a proper mvc config? In most cases, this will be enough:

 <mvc:annotation-driven />

(see this section)

d) I'm guessing you have to remove the @ResponseBody annotation. It's for direct output of a string, not for implicit conversion of an object.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

You need to set up a converter to convert the response to json, such as:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
   <property name="messageConverters">
      <list>
         <ref bean="jacksonJsonMessageConverter"/>
      </list>
   </property>
</bean>

This will use Jackson to convert the response to json. You'll need to add the Jackson lib as a dependency, more details can be found in this question. Jackson will convert any public fields, so you would need to add a getStrArr() method to Temp for strArr.

Community
  • 1
  • 1
slashnick
  • 26,167
  • 10
  • 55
  • 67