用 Java 技术创建 RESTful Web 服务(4)Providers
 
- UID
- 1066743
|

用 Java 技术创建 RESTful Web 服务(4)Providers
Providers JAX-RS 提供程序是一些应用程序组件,允许在三个关键领域进行运行时行为的定制:数据绑定、异常映射以及上下文解析(比如,向运行时提供 JAXBContext 实例)。每个 JAX-RS 提供程序类必须由 @Provider 注释。如下的例子讨论了两个数据绑定提供程序 MessageBodyWriter 和 MessageBodyReader。
MessageBodyWriter MessageBodyWriters 被 JAX-RS 运行时用来序列化所返回资源的表示。遵从 JSR-311 的运行时提供了对常见类型(java.lang.String、java.io.InputStream、 JAXB 对象等)的本机支持,但用户也可以向 JAX-RS 运行时提供他或她自己的 MessageBodyWriter。比如,您可以提供一个定制 MessageBodyWriter 来处理定制 ContactInfo Java 类型,如下所示。
清单 11. 定制 MessageBodyWriter1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| package com.ibm.jaxrs.sample.organization;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
@Provider
@Produces("text/xml")
public class ContactInfoWriter implements MessageBodyWriter<ContactInfo> {
public long getSize(T t, java.lang.Class<ContactInfo> type,
java.lang.reflect.Type genericType, java.lang.annotation.Annotation[]
annotations, MediaType mediaType) {
...
}
public boolean isWriteable(java.lang.Class<ContactInfo> type,
java.lang.reflect.Type genericType, java.lang.annotation.Annotation[]
annotations, MediaType mediaType) {
return true;
}
public void writeTo(ContactInfo contactInfo, java.lang.Class<ContactInfo> type,
java.lang.reflect.Type genericType, java.lang.annotation.Annotation[]
annotations, MediaType mediaType, MultivaluedMap<
java.lang.String, java.lang.Object> httpHeaders, java.io.OutputStream
entityStream) {
contactInfo.serialize(entityStream);
}
}
|
ContactInfoWriter 则在所返回的资源表示被序列化之前由 JAX-RS 运行时调用。如果 isWriteable 返回 true 且 @Produces 是此资源方法的 @Produces 值最为接近的匹配,就会调用 writeTo 方法。在这里,ContactInfoWriter 负责向底层的 OutputStream 序列化 ContactInfo 实例的内容。
MessageBodyReader MessageBodyReaders 则与 MessageBodyWriters 相反。对于反序列化,JAX-RS 运行时支持与序列化相同的类型。用户也可以提供他或她自己的 MessageBodyReader 实现。MessageBodyReader 的最主要的功能是读取请求 InputStream 并将传入的字节反序列化到一个此资源方法期望的 Java 对象。ContactInfo 类型的 MessageBodyReader 可以类似于清单 12。
清单 12. 定制 MessageBodyReader1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| package com.ibm.jaxrs.sample.organization;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;
@Provider
@Consumes("text/xml")
public class ContactInfoReader implements MessageBodyReader<ContactInfo> {
public boolean isReadable(java.lang.Class<ContactInfo> type,
java.lang.reflect.Type genericType, java.lang.annotation.Annotation[]
annotations, MediaType mediaType) {
return true;
}
public ContactInfo readFrom(java.lang.Class<ContactInfo> type,
java.lang.reflect.Type genericType, java.lang.annotation.Annotation[]
annotations, MediaType mediaType, MultivaluedMap<
java.lang.String,java.lang.String> httpHeaders, java.io.InputStream
entityStream) {
return ContactInfo.parse(entityStream);
}
}
|
与 MessageBodyWriter isWriteable 类似,ContactInfoReader 的 isReadable 方法将被调用以便决定 MessageBodyReader 能否处理此输入。如果 isReadable 返回 true 且 @Consumes 值与此资源方法的 @Consumes 值最为匹配,就会选择 ContactInfoReader。当 readFrom 方法被调用时,结果会是基于请求 InputStream 的内容创建 ContactInfo 实例。 |
|
|
|
|
|