1

我有一个带有一些方法的 RESTful 服务。方法由@POST和注释@Consumes(MediaType.APPLICATION_JSON)

我有请求参数的包装器:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class RequestWrapper {

    @XmlElement
    private SomeInfo someInfo = new SomeInfo();

    @XmlElement
    @XmlElementWrapper
    private List<RequestParameter> requestParameters = new ArrayList<>();
}

public class SomeInfo {
    public String field1;
    public String field2;
}

public class RequestParameter {
    public String key;
    public String value;
}

我向我的服务提出请求。POST 消息的正文:

{"someInfo":{"field1":"b","field2":"c"},"requestParameters":[{"key":"1","value":"2"},{"key":"3","value":"4"}]}

我看到someInfo通过 RequestWrapper 处理和访问的值。但是List<RequestParameter> requestParameters长度为零。

正常处理的消息怎么办?

PS 我使用 Glassfish 4.0。maven模块的依赖:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-processing</artifactId>
    <version>2.1</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.1</version>
    <scope>provided</scope>
</dependency>
4

1 回答 1

2

天哪,我找到了灵丹妙药

删除@XmlAccessorType(XmlAccessType.FIELD)

删除@XmlElementWrapper

为每个字段创建公共 getter/setter

@XmlRootElement
public class RequestWrapper {
    @XmlElement
    private SomeInfo someInfo = new SomeInfo();
    @XmlElement
    private List<RequestParameter> requestParameters = new ArrayList<>();

    // getters/setters for each field
}
于 2013-08-08T21:30:52.777 回答