[英]Dropwizard support for multiple serialization formats
It is pretty typical in a RESTful API to see endpoints that can support multiple serialization formats: 在RESTful API中,很典型的做法是查看可以支持多种序列化格式的端点:
// Sends back "fizz" resource that has an id=34 as a JSON object
http://api.example.com/v2/fizz/34.json
// Sends back "fizz" resource that has an id=34 as an XML object
http://api.example.com/v2/fizz/34.xml
// Sends back "fizz" resource that has an id=34 as a binary object,
// say, using Google Protocol Buffers
http://api.example.com/v2/fizz/34.bin
I'm designing a Dropwizard service, and am trying to figure out how to implement multiple format support, but the docs are barren in this regard. 我正在设计一个Dropwizard服务,并试图弄清楚如何实现多种格式支持,但是文档在这方面很贫乏。 Any ideas? 有任何想法吗?
Generally support for different format configuration is simply done declaratively in the @Produces
and @Consumes
annotations. 通常,仅在@Produces
和@Consumes
批注中声明性地完成对不同格式配置的支持。 When the client sends data, then the Content-Type
request header should be set to the actual type of the data being sent. 客户端发送数据时,应将Content-Type
请求标头设置为要发送的数据的实际类型。
For example if the client is sending JSON data, then the client should set the request header Content-Type: application/json
. 例如,如果客户端正在发送JSON数据,则客户端应设置请求标头Content-Type: application/json
。 Jersey will look for a method or class with @Consume("application/json")
. 泽西岛将使用@Consume("application/json")
查找方法或类。 If it finds it, then that means the app is configured to support the media type application/json
. 如果找到它,则意味着该应用已配置为支持媒体类型application/json
。 If not, then the client will get a response back saying the media type is not supported. 如果不是,则客户端将返回响应,说明不支持媒体类型。
Likewise when the client is requesting data, it should set the Accept: application/json
request header. 同样,当客户端请求数据时,客户端应设置Accept: application/json
请求标头。 Jersey will look for @Produces("application/json")
. 泽西岛将寻找@Produces("application/json")
。 If it can't find it for the endpoint, then the client will get a message saying it is not an acceptable type. 如果找不到端点,则客户端将收到一条消息,指出它不是可接受的类型。
So we can support different media types for the same endpoint. 因此,我们可以为同一端点支持不同的媒体类型。 You can declare the method like 您可以像这样声明方法
@Produces({"application/json", "application/xml", "application/x-protobuf"})
public Response getFoo() {
return Response.ok(new Foo());
}
The thing that you need to be concerned about is if there is a MessageBodyWriter
for each of those media types that can handle the serialization of the Foo
type. 您需要担心的是,对于每种可以处理Foo
类型序列化的媒体类型, 是否都有一个MessageBodyWriter
。 See more at JAX-RS Entity Providers . 在JAX-RS实体提供者中查看更多内容。
Alternatively, if the application/json
, application/xml
, application/x-protobuf
media types require different domain types to be serialized, you can have different methods to handle the different types. 另外,如果application/json
, application/xml
, application/x-protobuf
媒体类型需要序列化不同的域类型,则可以使用不同的方法来处理不同的类型。 For example, application/json
and application/xml
, you can usually get away with using the same Foo
domain object, so you can just do 例如, application/json
和application/xml
,通常Foo
使用相同的Foo
域对象,因此您可以
@Produces({"application/xml", "application/json"})
public Response getFooJsonOrXml() {
return Response.ok(new Foo());
}
But for Protobuf, it requires Protobuf compiled classes. 但是对于Protobuf,它需要Protobuf编译的类。 So instead of the Foo
domain object, you would return the generated type. 因此,您将返回生成的类型,而不是Foo
域对象。
@Produces("application/x-protobuf")
public Response getFooProtobuf() {
return Response.ok(new ProtobufFoo());
}
As for your use of the .xml
, .json
extension in your URLs, that is generally not the way for the client to say what type it wants. 至于您在URL中使用.xml
, .json
扩展名,通常这不是客户端说出所需类型的方式。 Usually the set the Accept
request header to one of the types that your server supports, as mentioned above. 如上所述,通常将“ Accept
请求”标头设置为服务器支持的一种类型。
But there is support for the URL type extension, but it is usually meant for clients that don't have access to setting the headers, for instance a browser. 但是它支持URL类型扩展,但这通常是为那些无权设置标头的客户端(例如浏览器)提供的。 But you need to configure those media type mappings with the UriConnegFilter
. 但是您需要使用UriConnegFilter
配置那些媒体类型映射。 For example 例如
Map<String, MediaType> map = new HashMap<>();
map.put("xml", MediaType.APPLICATION_XML_TYPE);
map.put("json", MediaType.APPLICATION_JSON_TYPE);
map.put("bin", ProtocolBufferMediaType.APPLICATION_PROTOBUF_TYPE);
env.jersey().property(ServerProperties.MEDIA_TYPE_MAPPINGS, map);
See Also: 也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.