我有一个实体类,看起来像这样。
@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 请求。