I am a new for Spring, using Spring 4.2.0. I want to return a JSON to client. It always return 406 error to client.
Here is my code,
@RequestMapping(
value = "/account",
method = RequestMethod.GET,
headers="Accept=*/*"
)
@ResponseBody
public Object account() throws ClientProtocolException, IOException{
JSONArray result = null;
HttpGet request = new HttpGet(baseURL+"/user/accountuser/2");
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
request.setHeader(HttpHeaders.AUTHORIZATION, authenCode);
HttpResponse response = client.execute(request);
String json = IOUtils.toString(response.getEntity().getContent());
try {
result = new JSONArray(json); // here is a json array from 3rd services
System.out.println(result);
return result;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
I tried solution from this question Spring MVC Controller Return JSON - Error 406 by using
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
It's still same.
After that I tried this solution spring mvc not returning json content - error 406 by using
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.3</version>
</dependency>
instead of before solution, but it's still same.
Moreover, I changed a lot of thing in the @RequestMapping such as
@RequestMapping(value = "/account",method = RequestMethod.GET,headers="Accept=*/*")
@RequestMapping(value = "/account",method = RequestMethod.GET,headers="Accept=application/json")
@RequestMapping(value = "/account",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
it's still same too.
I've faced on this problem for 2 day. I don't know how to solve it.
Thanks for help.