Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) returns JSON not XML representation #152

Open
clw275 opened this issue Dec 6, 2019 · 0 comments

Comments

@clw275
Copy link

clw275 commented Dec 6, 2019

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 [email protected] 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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant