简体   繁体   中英

How to set to default to json instead of xml in jersey?

使用jersey jersey.java.net当URI中没有接受标头或.xml后缀时,如何将JSON设置为默认序列化而不是XML?

You can assign the quality index to each media type in @Produces annotation. Ieyou can do the following to make Jersey prefer JSON if both XML and JSON are allowed:

@Produces({"application/json;qs=1", "application/xml;qs=.5"})

You should be able to set the @Produces annotation to specify the return format like so:

@Produces( { "application/json" })

How come there is no accepts header?

You can specify preference of generation by specifying media types in your order of preference in the @Produces annotation.

@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})

In the above code since "application/json" comes first, if no accept header is specified in the request Jersey will default to generating JSON response.

Using qs (as suggested by Martin) makes the preference more explicit, but its a bit more complicated to understand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM