什么是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;
     }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇