什么是JAXB和JAXRS?他们怎么相关?

前端之家收集整理的这篇文章主要介绍了什么是JAXB和JAXRS?他们怎么相关?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对不起这个钝的问题.但是,很多人一天天使用这两个词,但我不知道,我做了一些研究,知道它是分开的.但不明白它是如何相关的.我将分享我对这两个知识的理解.

JAXB is XML-to-Java binding technology enabling transformations
between schema and Java objects and between XML instance documents
and Java object instances. Internally JAXB does all this conversions
between xml and java . This is a parser of xml and then it knows what
component in xml corresponds to what in java and it breaks .
Conversion of this answer from JAXB is done by tools like xjc ( or
codgen plugin) . Mapping may be like

xsd:string java.lang.String

xsd:integer java.math.BigInteger

JaxRs is different . This is set of specifications for handling
requests . Meaning that it says “GET(“/foo”) ” means handle a get
call with url /foo . It only states that . How it is done ? Yes,that
is called implementation of this spec . There are number of
implementations like restlet,resteasy,jersey,apache cxf etc .
This is analogus to logic and way you implement in maths . the
algorithm idea is bucket search .This can be implemented in any way .
In java terms JaxRs is interface and these 4 restlet,
jersey,apache cxf are implementations of the interface .

现在请说明我的理解是否正确.然后告诉他们如何相关.请帮忙 .如果可能的话,图片说明会更有帮助.

你的理解基本正确. JAXB和JAX-RS都是具有多个实现的Java Community Process(JCP)标准.

JAXB – 定义用于将Java域对象转换为/从XML转换的标准化元数据和运行时API.

JAX-RS – 定义用于创建RESTful服务的标准化元数据和运行时API.默认情况下,应用程序/ xml媒体类型JAX-RS将使用JAXB将对象转换为/从XML转换.

在以下示例中,当执行GET操作时,JAX-RS实现将返回一个Customer.将使用JAXB impl将该客户端实例转换为客户端实际接收的XML.

package org.example.service;

import javax.ejb.*;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import org.example.model.*;

@Stateless
@LocalBean
@Path("/customers")
public class CustomerResource {

    @GET
    @Produces(MediaType.APPLICATION_XML)
    @Path("{id}")
    public Customer read(@PathParam("id") int id) {
        Customer customer = new Customer();
        customer.setId(id);
        customer.setFirstName("Jane");
        customer.setLastName(null);

        PhoneNumber pn = new PhoneNumber();
        pn.setType("work");
        pn.setValue("5551111");
        customer.getPhoneNumbers().add(pn);

        return customer;
     }

}
原文链接:https://www.f2er.com/xml/292186.html

猜你在找的XML相关文章