Issue
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.
Solution
My solution is based on Spring's RestTemplate
. It is a convenient approach to talk to remote services. Various HttpMessageConverter
transform request/response objects into common formats like XML or JSON out of the box as long as the necessary library is available on classpath.
Your code even doesn't require Jackson
because it acts as a proxy for a third party service. Please keep in mind. This is a demo and not meant to be used in a production system as is.
@Controller
public class DemoController {
private final RestTemplate restTemplate = new RestTemplate();
private final String baseUrl = "http://localhost:8080";
@RequestMapping(value = "/account", method = GET, produces = APPLICATION_JSON_VALUE)
public void account(HttpServletResponse response) throws Exception {
ResponseEntity<Resource> exchange = restTemplate.exchange(baseUrl + "/user/accountuser/2", HttpMethod.GET, createEntity(), Resource.class);
IOUtils.copy(exchange.getBody().getInputStream(), response.getOutputStream());
}
private HttpEntity<String> createEntity() {
final HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(AUTHORIZATION, "someCode");
httpHeaders.setContentType(APPLICATION_JSON);
return new HttpEntity<String>(httpHeaders);
}
}
Answered By - ksokol
Answer Checked By - Mary Flores (JavaFixing Volunteer)