简体   繁体   English

编组列表 <String> 与JAX-RS

[英]Marshalling List<String> with JAX-RS

I'm used to working with jax-ws where a wsdl file is generated, and a client can then be generated based on this wsdl file and its xsd(s) using a maven plugin. 我习惯使用jax-ws生成wsdl文件,然后可以使用maven插件基于此wsdl文件及其xsd生成客户端。 Using this client is no hassle at at, and you don't have to really think about what happens in the background, like marshalling, http transfer and such. 使用此客户端毫不费力,您也不必真正考虑后台发生的事情,例如编组,http传输等。

I'm currently working on a jax-rs project using jaxb to unmarshal objects. 我目前正在使用jaxb解组对象的jax-rs项目。 One of the methods there returns a list of strings, but it seems that jaxb does not know how to marshal this , which is kinda surprising as it does know how to marshal a list of entities (ex, customers). 那里的一种方法返回一个字符串列表,但是jaxb似乎不知道如何封送此字符串,这有点令人惊讶,因为它确实知道如何封送一个实体列表(例如,客户)。

Also, I have written a client for the jax-rs service on my own, handling both http responses and unmarshalling of the payload using jaxb. 另外,我自己编写了一个jax-rs服务的客户端,使用jaxb处理http响应和有效载荷的编组。 Marshalling and unmarshalling with jaxb is a real hassle since it cannot automatically marshall or unmarshall list of entities that is added to its context, even less lists of strings. 使用jaxb进行编组和解组是一个真正的麻烦,因为它无法自动编组或解组添加到其上下文中的实体列表,甚至更少的字符串列表。

I would like to know if there is some neat way to get all of this for free using restful webservices? 我想知道是否有一些巧妙的方法可以使用宁静的Web服务免费获取所有这些资源? This would have to be quite lightweight, and the clients must be easy to distribute. 这必须非常轻巧,并且客户端必须易于分发。

Thanks! 谢谢! Runar Runar

The service method that is not working using jaxrs and jaxb: 使用jaxrs和jaxb不能使用的服务方法:

@GET
@Path("/{customerId}")
@Produces(MediaType.APPLICATION_XML)
public List<String> isCustomerLocked(@PathParam("customerId") Long customerId) {


}

Client code that attempts to marshall/unmarshall text payload. 尝试编组/解组文本有效内容的客户端代码。 Classes added to the jaxbcontext not shown: 未显示添加到jaxbcontext中的类:

javax.xml.bind.Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(obj, stringwriter)

javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.unmarshal(inputstream)

I'd use JAXB to wrap the data. 我会用JAXB包装数据。 For a simple List<String> this may look as overkill. 对于一个简单的List<String>这可能看起来有些过头了。 But in most cases you want to un-/marshall Resource Representations, not simple objects. 但是在大多数情况下,您要取消/编组资源表示,而不是简单的对象。

Remember: REST ist not RPC! 切记:REST 不是 RPC!

If you really want tom un-/marshall List<String> write a JAX-RS Provider. 如果您真的想取消/编组List<String>编写一个JAX-RS提供程序。 But I'd prefer using JAXB. 但是我更喜欢使用JAXB。

S.java

@XmlRootElement
public class S {

    private String s;

    public S() {
    }

    public S(String s) {
        this.s = s;
    }

    public String getS() {
        return s;
    }

    public void setS(String s) {
        this.s = s;
    }
}

Ss.java

@XmlRootElement(name="ss-wrapper")
public class Ss {

    private List<S> ss;

    public List<S> getSs() {
        return ss;
    }

    public void setSs(List<S> ss) {
        this.ss = ss;
    }

    public Ss(List<S> ss) {
        this.ss = ss;
    }

    public Ss() {
    }
}

JAX-RS class JAX-RS类

@Path("/strings")
@GET
@Produces(MediaType.APPLICATION_XML)
public Response getListOfStrings() {
    S s1 = new S("foo");
    S s2 = new S("bar");
    List<S> strings = new ArrayList<S>();
    strings.add(s1);
    strings.add(s2);
    Ss ss = new Ss(strings);
    return Response.ok(ss).build();
}

XML XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ss-wrapper>
  <ss>
    <s>foo</s>
  </ss>
  <ss>
    <s>bar</s>
  </ss>
</ss-wrapper>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM