@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) returns JSON as default

108 views
Skip to first unread message

Charissa Willard

unread,
Dec 5, 2019, 2:24:25 PM12/5/19
to Genson user group
I am using genson 1.4 and jersey 2.12. I also added the @XmlRootElement annotation to the Java class I'm using for the response object. I set the @Produces annotation to handle both XML & JSON such that I get XML back as the default. Technically, the first one in the @Produces annotation list should be used 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. Also, if I just specify @Produces({ MediaType.APPLICATION_XML}), I get back the XML representation. Does genson default to the first item specified in the @Produces annotation?

I also specify -H "Content-Type: application/json" -in the curl command since I'm sending JSON in the entity object.

Eugen Cepoi

unread,
Dec 6, 2019, 12:13:45 AM12/6/19
to gen...@googlegroups.com
Hi Charissa,

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 (+some tests to confirm it addresses your use case). If all looks good I could merge and do a release with your changes.


Le jeu. 5 déc. 2019 à 11:24, Charissa Willard <charissa...@comcast.net> a écrit :
I am using genson 1.4 and jersey 2.12. I also added the @XmlRootElement annotation to the Java class I'm using for the response object. I set the @Produces annotation to handle both XML & JSON such that I get XML back as the default. Technically, the first one in the @Produces annotation list should be used 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. Also, if I just specify @Produces({ MediaType.APPLICATION_XML}), I get back the XML representation. Does genson default to the first item specified in the @Produces annotation?

I also specify -H "Content-Type: application/json" -in the curl command since I'm sending JSON in the entity object.

--
You received this message because you are subscribed to the Google Groups "Genson user group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to genson+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/genson/e78b43ec-8f48-4426-9614-aebba05ec758%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages