0

我有一个实体类,看起来像这样。

@XmlRootElement
public class ImageSuffix {

    @XmlAttribute
    private boolean canRead;

    @XmlAttribute
    private boolean canWrite;

    @XmlValue;
    private String value;
}

我写的一个 JAX-RS 资源类看起来像这样。

@Path("/imageSuffixes")
public class ImageSuffixesResource {

    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public List<ImageSuffix> read() {
        // ...
    }

    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})    
    @PUT
    public void update(final List<ImageSuffix> imageSuffixes) {
        // ...
    }

    @GET
    @Path("/{name: .+}")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Response readImageSuffix(@PathParam("name") final String name) {
        // ...
    }

    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @PUT
    @Path("/{name: .+}")
    public void updateImageSuffix(@PathParam("name") final String name,
                                  final ImageSuffix imageSuffix) {
        // ...
    }
}

这是结果

GET /imageSuffixes     in application/xml
PUT /imageSuffixes     in application/xml
GET /imageSuffixes/png in application/xml
PUT /imageSuffixes/png in application/xml

GET /imageSuffixes     in application/json
PUT /imageSuffixes     in application/json FAIL: 400
    -> The request sent by the client was syntactically incorrect (Bad Request)
GET /imageSuffixes/png in application/json
PUT /imageSuffixes/png in application/json FAIL: fields don't get values

这里是/imageSuffixes in application/json.

{
    "imageSuffix": [
        {
            "$": "jpg",
            "@canRead": "true",
            "@canWrite": "true"
        },
        {
            "$": "bmp",
            "@canRead": "true",
            "@canWrite": "true"
        },
        {
            "$": "wbmp",
            "@canRead": "true",
            "@canWrite": "true"
        },
        {
            "$": "jpeg",
            "@canRead": "true",
            "@canWrite": "true"
        },
        {
            "$": "png",
            "@canRead": "true",
            "@canWrite": "true"
        },
        {
            "$": "gif",
            "@canRead": "true",
            "@canWrite": "true"
        }
    ]
}

这是/imageSuffixes/png in application/json.

{
    "$": "png",
    "@canRead": "true",
    "@canWrite": "true"
}

这是正常的还是泽西的错?

当我依赖以下依赖项时,它适用于一些不同的 JSON 输出。

<dependency>
  <groupId>com.fasterxml.jackson.jaxrs</groupId>
  <artifactId>jackson-jaxrs-json-provider</artifactId>
  <version>2.1.4</version>
</dependency>
{
    "canRead": true,
    "canWrite": true,
    "value": "png"
}

真正的问题是 GlassFish(带球衣)没有解析他(或她)打印出来的 JSON 请求。

4

1 回答 1

1

通过细微的更改,您的代码对我来说就可以了。请注意,在您当前的配置中,您正在使用 Jackson 库来处理传入/传出的 JSON 消息。默认情况下,在反序列化数据时,如果遇到未知属性(即 JSON 中存在的属性,在 Java 对象上没有相应的属性映射),Jackson 将失败。避免此错误的一种方法是使用@@JsonIgnoreProperties注释。

@XmlRootElement
@JsonIgnoreProperties(ignoreUnknown=true)
public class ImageSuffix {

    @XmlAttribute
    private boolean canRead;

    @XmlAttribute
    private boolean canWrite;

    @XmlValue;
    private String value;
}

似乎在测试接受值列表的资源方法时,您发送的是单个对象。确保发送一个实际的 JSON 数组:

[
    {
        "$": "jpg",
        "@canRead": "true",
        "@canWrite": "true"
    },
    {
        "$": "bmp",
        "@canRead": "true",
        "@canWrite": "true"
    }
]

最后,在您使用的任何测试客户端中,不要忘记添加请求标头Accept: application/jsonContent-Type: application/json.

于 2013-04-10T02:36:49.253 回答