-
Notifications
You must be signed in to change notification settings - Fork 65
Description
I am using genson 1.4 and jersey 2.12. I set the @produces annotation to handle both XML & JSON such that I get XML back as the default. Technically, the first type in the @produces annotation list should be used for the returned format if the Accept header isn't specified. However, I'm getting JSON back if the Accept header with application/xml isn't included in the curl command. If I specify -H "Accept: application/xml" in the curl command, I get back the XML representation (see example below). Also, if I just specify @produces({ MediaType.APPLICATION_XML}), I get back the XML representation.
Example:
@GET
@Path("/hostname")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public HostName getHostName() {
...
return hostName;
}
$ curl http://.../hostname
{"domain":"domain.dev","name":"gw123"}
$ curl -H "Accept: application/xml" http://.../hostname
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><hostname><domain>domain.dev</domain><name>gw123</name></hostname>
Email from Eugen Cepoi cepoi.eugen@gmail.com below written on Dec 5, 2019 suggests to return false in isWriteable/isReadable if mediaType != MediaType.APPLICATION_JSON_TYPE in GensonJsonConverter:
I suspect maybe there is something weird in how Jersey is using these annotations and it might be because we don't check the media type in here https://github.com/owlike/genson/blob/master/genson/src/main/java/com/owlike/genson/ext/jaxrs/GensonJsonConverter.java#L65-L70.
Feel free to open a PR that would check the media type in isWriteable/isReadable to return false if it's not JSON
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType) {
if (mediaType != MediaType.APPLICATION_JSON_TYPE)
return false;
GensonJaxRSFeature feature = _gensonResolver.getContext(type);
return feature.isEnabled() && feature.isSerializable(type);
}
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType) {
if (mediaType != MediaType.APPLICATION_JSON_TYPE)
return false;
GensonJaxRSFeature feature = _gensonResolver.getContext(type);
return feature.isEnabled() && feature.isDeserializable(type);
}