私はこのようなエンティティクラスを持っています。
@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 リクエストを解析しないということです。これはまさに彼 (または彼女) が出力したものです。