【发布时间】:2013-04-10 01:43:32
【问题描述】:
我有一个像这样的实体类。
@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 请求。
【问题讨论】:
-
我不确定@Path("/{name: .+}") 是否是正确的语法。你试过@Path("/name") "
-
服务器日志中的错误信息是什么?
-
@Abraham 路径表达式没问题,我相信。
-
@Perception
The request sent by the client was syntactically incorrect (Bad Request). -
好吧,我将假设对于您的集合 put,您传递的是内部数组(与
imageSuffix关联的元素),因为外部结构是一个 JSON 对象。当你越过这一点时,你会遇到 JSON 中的$属性无法映射到你的 POJO 的问题。
标签: json jaxb glassfish jersey jax-rs