Reputation: 81
I'm using spring 3.0.0.
I have an endpoint that returns an object that I want to be serialized to JSON. When the request comes in with Accept: application/json, it works correctly. The request is currently coming in with */*
as the Accept value. Unfortunately I don't have control over the request, otherwise I would change that. When */*
is received, it throws a HttpMediaTypeNotAcceptableException exception.
Is there a way to map this accept pattern to application/json?
This is very similar to another question, but the key difference is I need to have the Accept header be */*
. Spring's Json not being resolved with appropriate response
Here's what my controller looks like:
@RequestMapping(value = "/v1/endpoint", method = RequestMethod.POST)
@ResponseBody
public EndpointResponse runEndpoint(@RequestBody String jsonData) {
ObjectMapper mapper = new ObjectMapper();
EndpointRequest opRequest = null;
EndpointResponse opResponse = null;
try {
opRequest = mapper.readValue(jsonData, EndpointRequest.class);
//....do stuff
} catch (JsonParseException e) {
return handleException(opResponse, e);
} catch (JsonMappingException e) {
return handleException(opResponse, e);
} catch (IOException e) {
return handleException(opResponse, e);
}
return opResponse;
}
Thanks!
Upvotes: 2
Views: 11469
Reputation: 81
From everything I've gathered today there are 2 options to get around this issue.
Write a BeanPostProcessor that will update the supported mime types on the MappingJacksonConverter to accept /. (using the supportedMimeTypes property just doesn't work....see https://jira.springsource.org/browse/SPR-6214
Here's the solution I'm going with for now. It's not pretty, but neither is setting an Accept header of / when you are expecting application/json everytime. I ended up adding the ServletResponse object to my method and using the old school way of writing directly to the output stream to bypass the message converters and get my response returned.
@RequestMapping(value = "/v1/endpoint", method = RequestMethod.POST)
public void runEndpoint(@RequestBody String jsonData,
ServletResponse response) {
ObjectMapper mapper = new ObjectMapper();
EndpointRequest opRequest = null;
EnpointResponse opResponse = null;
StringWriter sw = new StringWriter();
try {
opRequest = mapper.readValue(jsonData, EndpointRequest.class);
opResponse = ...do stff...
mapper = new ObjectMapper();
mapper.writeValue(sw, opResponse);
response.setContentType("application/json");
response.getOutputStream().write(sw.toString().getBytes());
} catch (JsonParseException e) {
handleException(opResponse, e, response);
} catch (JsonMappingException e) {
handleException(opResponse, e, response);
} catch (IOException e) {
handleException(opResponse, e, response);
}
}
If you have anything more elegant, I'd love to see it!
Upvotes: 2
Reputation: 2021
Include these dependencies to your pom.xml
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.3</version>
</dependency>
Upvotes: 0
Reputation: 422
I just ran into this same issue. I'm not sure if its more elegant but what I've chosen to do is to just return a string, and convert the object to Json myself. I decided to use Gson for the JSON library.
@Controller
public class NewAbstractController {
@RequestMapping(value = "/simple", method = RequestMethod.GET)
public @ResponseBody String SayHello()
{
Foo foo = new Foo();
foo.Name = "Chris";
foo.age = 30;
Gson gson = new Gson();
return gson.toJson(foo);
}
public class Foo implements Serializable
{
public String Name;
public Integer age;
}
}
Upvotes: 1