Issue
I use "Jsonitter" as a JSON serialization framework and I don't use Maven in my project. I've been returning JSON objects in in my restful api by directly writing the result of the "Jsonitter" to the HttpServletResponse
until now that I found about the @RestController
attribute. Being from an ASP.Net MVC background, I expect the framework to automatically serialize the returned object in my api in accordance with the Accept
header. But I feel like, Spring requires a third-party serialization framework to render the result (i.e. Jackson) because it returns HTTP Status 406 - Not Acceptable
result instead.
The way I'm using it is as follows:
@RestController
@EnableWebMvc
public class TestApi {
@RequestMapping(value = "Test", method = RequestMethod.Get, produces = "application/json")
public List<String> letsTest(){
return myStringList;
}
}
I don't have any reference to Jackson and I'd rather not use it at all and I feel like the error is due to that. Is there any way to get this work without Jackson?
Solution
Regarding 406 - Not Acceptable
Since your controller is configured to only return a response if the client requests application/json
data,
produces = "application/json"
you probably get "406 Not Acceptable" because your client requested something else / did not specify any Accepts header. You can fix by:
- fixing your client request to provide
Accepts
header - changing the annotation to accept whatever your clients send (inluding
*/*
if you are lazy).
This is your only issue, you are not forced to use any Serialization framework with Spring (But a Serializer is required if you define your controller methods to return arbitrary beans). You can write any String as a response to your clients.
Regarding how to respond with Jsoniter in the background
If you want to keep using Jsoniter, but move that to the background so your Controller class does not explicitly mention jsoniter anymore, you need to define your own custom HttpMessageConverter.
Regarding how to respond without Jsoniter
You need some Serializer to generate Json from Java. You can write custom code, or use Jackson or Gson. Spring supports Jackson by default, and Gson by using GsonHttpMessageConverter. For Jackson, your current code is ok, but you need to add a jackson dependency. For Gson, you need to declare the converter, there are several resources explaining that online.
Regarding how to manually respond with Jsoniter
As in the answers to this question Does spring mvc have response.write to output to the browser directly?
You can write your response using @ResponseBody
. Since @RestController
already implies @ResponseBody
, you could also leave it out. Showing it here just for clarification:
@ResponseBody // not needed with @RestController
@RequestMapping(value = "Test", method = RequestMethod.Get, produces = "application/json")
public String letsTest() {
// Using Jsoniter JsonStream
return JsonStream.serialize(myStringList);
}
Answered By - tkruse
Answer Checked By - Gilberto Lyons (JavaFixing Admin)