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

マイナーな変更により、コードは私にとっては問題なく機能します。現在の構成では、受信/送信 JSON メッセージの処理に Jackson ライブラリを使用していることに注意してください。デフォルトでは、データをデシリアライズするときに、Jackson が不明なプロパティ (つまり、JSON に存在し、Java オブジェクトに対応するプロパティ マッピングを持たない属性) に遭遇すると失敗します。このエラーを回避する 1 つの方法は、@@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 に答える