XML和JSON Web API:从POJO自动映射? [英] XML & JSON web api : automatic mapping from POJOs?

查看:113
本文介绍了XML和JSON Web API:从POJO自动映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将开始一个小项目,目标是最终获得一个Web xml/json API.我将用Java编写它,并使用restlet库.

I'm about to start on a small project which goal is to end up with a web xml/json api. I'll be writing it in Java, and I'll be using the restlet library.

如何处理xml/json对偶?我知道我可以使用JAXB将pojos转换"为xml(然后转换为xml),但是如何为json自动化呢? restlet库中可以使用任何功能吗?

How do I approach the xml/json duality? I know I can use JAXB to "convert" pojos to xml (and back), but how do I automate this for json? Is there any functionality in the restlet library I can leverage?

推荐答案

Restlet允许您在REST注释方法级别直接在服务器资源中使用POJO,如下所述:

Restlet allows you to directly work with POJOs within your server resources at the level of REST annotated methods, as described below:

public class MyServerResource extends ServerResource {
    @Get
    public List<MyPojo> getList() {
        List<MyPojo> list = (get list from backend for example)
        return list;
    }

    @Post
    public MyPojo addPojo(Pojo pojo) {
        addPojoInBackend(pojo

        getResponse().setLocationRef(getUri() + field.getId());
        getResponse().setStatus(Status.SUCCESS_CREATED);

        return pojo;
    }
}

您无需在注释内容中指定媒体类型(即内容类型).

You don't need to specify media type (i.e. content type) within the content of the annotations.

为处理此类代码,Restlet提供了一种通用的转换功能,该功能可以在后台进行工作. Restlet扩展为此功能提供了不同的实现.如果您想同时为RESTful应用程序同时使用XML和JSON,我认为您应该针对自己想做的事情使用Jackson扩展.杰克逊( http://wiki.fasterxml.com/JacksonHome )是一种可以将POJO转换为各种格式,例如XML,JSON和YAML.

To handle such code, Restlet provides a generic conversion feature that does the work under the hood. Restlet extensions provide different implementations for this feature. If you want to have both XML and JSON for your RESTful application, I think that you should the Jackson extension for what you want to do. Jackson (http://wiki.fasterxml.com/JacksonHome) is a tool that allows to convert POJO to various formats like XML, JSON and YAML.

要启用此功能,只需将Jackson扩展名的JAR文件(org.restlet.ext.jackson)放在类路径中,然后使用上述编程方法即可.以下是有关其工作原理的详细信息:

To enable this, simply put the JAR file of the Jackson extension (org.restlet.ext.jackson) in your classpath and use the programming approach described above. Here are the details on how it works:

  • 您将能够发送JSON和XML内容(在请求中设置标头Content-Type),Restlet会自动将此内容转换为带注释的方法中期望的bean.

  • You will be able to send both JSON and XML content (set the header Content-Type in the request) and Restlet will automatically convert this content to the bean expected in the annotated method.

POST /myresource
Content-type: application/json
{ "id":"myid", ... }

  • 要切换/选择带有响应的预期内容,您可以根据头文件Accept进行连接(REST的内容协商).如果指定application/json,将接收JSON内容,而applicaiton/xml是XML内容.

  • To switch / select the expected content with response, you can leverage to conneg (content negociation of REST) based on the header Accept. If you specify application/json, a JSON content wil be received and applicaiton/xml, a XML one.

    GET /myresource
    Accept: application/json
    { "id":"myid", ... }
    
    GET /myresource
    Accept: application/xml
    <elt><id>myid> ... </elt>
    

  • 您会注意到Restlet还支持查询参数media(非标准)来选择要接收的内容.如果指定json,将返回一个JSON内容,而xml一个XML内容.

  • You can notice that Restlet also support a query parameter media (not standard) to select the content to receive. If you specify json, a JSON content will be received back and xml an XML one.

    GET /myresource?media=json
    { "id":"myid", ... }
    
    GET /myresource?media=xml
    <elt><id>myid> ... </elt>
    

  • 最后,您会注意到Restlet在服务器端也支持此机制.这意味着您可以直接使用bean.该功能可以与带有Restlet注释的接口一起使用,如下所述:

    To finish, you can notice that this mechanism is also supported on server-side with Restlet. This means that you can work directly with beans. This feature can be used with Restlet annotated interfaces, as described below:

    public interface MyResource {
        @Get
        List<MyPojo> getList();
    
        @Post
        MyPojo addPojo(Pojo pojo);
    }
    

    您可以按如下所述使用此界面:

    You can use this interface as described below:

    ClientResource cr = new ClientResource("http://(...)/myresource");
    MyResource myResource = cr.wrap(MyResource.class);
    // List
    List<Pojo> list = myResource.getList();
    // Add
    Pojo pojo = new Pojo();
    pojo.setId("myid"); // for example
    (...)
    Pojo returnedPojo = myResource.add(pojo);
    

    别忘了在客户端类路径应用程序中添加扩展名Jackson.

    Don' forget to put in the client classpath application the extension Jackson.

    希望有帮助, 蒂埃里

    这篇关于XML和JSON Web API:从POJO自动映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆