【问题标题】:Jersey Client to save response from REST Call into custom objectJersey 客户端将来自 REST 调用的响应保存到自定义对象中
【发布时间】:2014-10-18 01:41:56
【问题描述】:

我的REST 服务输出@Produces(MediaType.APPLICATION_XML)。这是从:

ArrayList<CoffeeOrder> orders = new ArrayList<CoffeeOrder>();

GenericEntity<ArrayList<CoffeeOrder> > entity = new GenericEntity<ArrayList<CoffeeOrder> >(orders) {};

 ....

return Response.status(Response.Status.OK).entity(entity).build();  

Example:

<coffeeOrders>
  <coffeeOrder>
    <id>2</id>
    <links>http://localhost:9080/cs9322.ass2/rest/coffee/2</links>
    <links>http://localhost:9080/cs9322.ass2/rest/payment/2</links>
  </coffeeOrder>
<coffeeOrder>
  <id>1</id>
  <links>http://localhost:9080/cs9322.ass2/rest/coffee/1</links>
  <links>http://localhost:9080/cs9322.ass2/rest/payment/1</links>
</coffeeOrder>

在我的client 中,我想调用此服务并将响应保存为ArrayList&lt;CoffeeOrder&gt;

CoffeeOrder 看起来像这样:

public class CoffeeOrder {

   private String id;

   private ArrayList<String> links = new ArrayList<String>();

   ...
}

有没有办法用来自REST 请求的响应填充这个对象?

感谢您的帮助!

【问题讨论】:

    标签: java rest jersey


    【解决方案1】:

    我的建议是使用JAXB annotations。正如Jersey Tutorial 所述,“Jersey 包含对可以将 JAXB bean 序列化为 XML 的实体提供程序的默认支持”

    因此您可以创建一个 CoffeeOrders 类,其中包含一个 List&lt;CoffeeOrder&gt;。类似的东西

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class CoffeeOrders {
    
        @XmlElement(name = "coffeOrder")
        protected List<CoffeeOrder> coffeeOrders;
    
        // GETTER and SETTERS
    
        public void addCoffeeOrder(CoffeeOrder coffeeOrder) {
            if (coffeeOrders == null) {
                coffeeOrders = new ArrayList<>();
            }
            coffeeOrders.add(coffeeOrder);
        }
    }
    

    你的回答可能只是

    CoffeeOrders orders = new CoffeeOrders();
    // add CoffeeOrder(s) to list in orders
    return Response.ok(orders).build();
    

    &lt;links&gt; 而言,您似乎正在尝试实现一些HATEOAS 格式。我建议也许使用Link 类来创建链接。您可以在CoffeeOrder 类中拥有List&lt;Link&gt;

    @XmlAccessorType(XmlAccessType.FIELD)
    public class CoffeeOrder {
    
        @XmlElement
        protected String id;
    
        @XmlElement(name = "link")
        @XmlJavaTypeAdapter(Link.JaxbAdapter.class)
        protected List<Link> links;
    
        // GETTERS and SETTERS
    }
    

    而且(不是很好)响应的示例可能类似于

    @Path("/coffee")
    public class CoffeeResource {
    
        @GET
        @Produces(MediaType.APPLICATION_XML)
        public Response getCoffeOrders(@Context UriInfo uriInfo) {
            UriBuilder builder = uriInfo.getBaseUriBuilder();
            CoffeeOrders orders = new CoffeeOrders();
    
            CoffeeOrder order = new CoffeeOrder();
            order.setId("1");
    
            List<Link> links = new ArrayList<>();
            URI coffee = builder.clone().path("coffee").path("1").build();
            Link coffeeLink = Link.fromUri(coffee).rel("coffee1")
                    .type(MediaType.APPLICATION_XML).build();
            links.add(coffeeLink);
    
            URI payment = builder.clone().path("payment").path("1").build();
            Link paymentLink = Link.fromUri(coffee).rel("payment1")
                    .type(MediaType.APPLICATION_XML).build();
            links.add(coffeeLink);
    
            order.setLinks(links);
            orders.addCoffeeOrder(order);
    
            return Response.ok(orders).build();
        }
    }
    

    接收Response 可能看起来像

    @Test
    public void testGetIt() throws Exception {
        Response response = target.path("coffee").request().get();
        CoffeeOrders order = response.readEntity(CoffeeOrders.class);
        response.close();
        JAXBContext context = JAXBContext.newInstance(CoffeeOrders.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(order, System.out);
    }
    

    结果

    <coffeeOrders>
        <coffeOrder>
            <id>1</id>
            <link href="http://localhost:8080/myapp/coffee/1" rel="coffee1" type="application/xml"/>
            <link href="http://localhost:8080/myapp/coffee/1" rel="coffee1" type="application/xml"/>
        </coffeOrder>
    </coffeeOrders>
    

    要接收带有CoffeeOrders 对象的 POST,只需使用相同的 xml 接受类型。 Jersey 运行时将根据您提供的 JAXB 注释为您解析 xml。


    一些资源:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 2018-02-12
      相关资源
      最近更新 更多