1)什么是JaxB提供程序,它与ContextResolver相同?
2)什么是jaxb处理程序?
我在这些术语中非常迷失。请回答简单易懂的单词。
这是来自书:
JAXB JAX-RS处理程序
JAX-RS规范要求实现自动支持使用@XmlRootElement或@XmlType注释的类以及包装在javax.xml.bind.JAXBElement实例中的对象的编组和解组。这是一个使用前面定义的Customer类进行交互的示例:
@Path("/customers")
public class CustomerResource {
@GET
@Path("{id}")
@Produces("application/xml")
public Customer getCustomer(@PathParam("id") int id) {
Customer cust = findCustomer(id);
return cust;
}
@POST
@Consumes("application/xml")
public void createCustomer(Customer cust) {
...
} }
如您所见,一旦将JAXB注释应用于Java类,就可以非常轻松地在客户端和Web服务之间交换XML文档。内置的JAXB处理程序将处理application / xml,text / xml或application / * + xml媒体类型的任何JAXB注释类。默认情况下,它们还将管理JAXBContext实例的创建和初始化。由于JAXBContext实例的创建可能很昂贵,因此JAX-RS实现通常在首次初始化后对其进行缓存。 使用ContextResolvers管理您自己的JAXBContexts
如果您已经熟悉JAXB,那么您将知道很多时候需要以某种方式配置JAXBContext实例以获得所需的输出。 JAX-RS内置JAXB提供程序允许您插入自己的JAXBContext实例。它的工作方式是你必须实现一个名为javax.ws.rs.ext.ContextResolver的类似工厂的接口来覆盖默认的JAXBContext创建:
public interface ContextResolver<T> {
T getContext(Class<?> type);
}
ContextResolvers are pluggable factories that create objects of a specific type, for a certain Java type, and for a specific media type. To plug in your own JAXBContext, you will have to implement this interface. Here’s an example of creating a specific JAXBContext for our Customer class:
@Provider
@Produces("application/xml")
public class CustomerResolver
implements ContextResolver<JAXBContext> {
private JAXBContext ctx;
public CustomerResolver() {
this.ctx = ...; // initialize it the way you want
}
public JAXBContext getContext(Class<?> type) {
if (type.equals(Customer.class)) {
return ctx;
} else {
return null;
}
}
}
答案 0 :(得分:3)
JAXB是“Java Architecture for XML Binding”的首字母缩写,这是一种定义XML文档和Java对象树之间转换的方法的规范,最初由Sun Microsystems创建。有效规范2.0版于2006年完成。
根据JAXB规范的实现是 JAXB提供程序。
规范包含一些提示,可能的实现可能包含的内容。例如:“JAXBContext类是Java应用程序的入口点 JAXB框架。“它维护有关在(非)编组期间期望的类的信息。它可以从一个或多个包或类列表中创建。(上下文解析的过程可能遵循注释中的提示。)
术语“JAXB处理程序”(因为它在引用文本中使用)是指与JAXBContext类关联的代码,该类调查Java类,内省字段和方法以及注释,从而创建包含在其中的所有信息的数据库Java代码。
答案 1 :(得分:3)
JAXB提供程序是Java Architecture for XML Binding (JSR-222)规范的实现。该规范是通过Java Community Process创建的。它最初由Sun Microsystems领导,但现在由Oracle领导。专家组的成员来自几个对象到XML技术(XMLBeans,EMF,TopLink OX等)以及几个人。需要JAXB实现才能通过测试兼容性工具包(TCK)。以下是几个JAXB提供商的链接:
JAXB是JAX-RS中的默认对象到XML提供程序。默认情况下,它将根据JAX-RS注释方法的参数/返回类型创建JAXBContext
(即使用@GET
注释)。然后它将引入所有引用的类以生成元数据。有时,这不会产生所有必需的元数据,您需要自己提供JAXBContext
。这可以使用ContextResolver
来完成。
我不熟悉这个词。