1

こんにちは、jersey サービスを介して以下の json オブジェクトを変換しようとしています。

{
    "User": {
        "username": "newusername",
        "password": "newpassword",
        "email": "test.test@test.com",
        "address": "test",
        "firstName": "test",
        "lastName": "test",
        "city": "test"
    }
}

以下のエラーが表示されます:

Unrecognized field "User" (Class com.test.webservice.bean.User), not marked as ignorable

 at [Source: io.undertow.servlet.spec.ServletInputStreamImpl@1dad89c0; line: 11, column: 2] (through reference chain: com.test.webservice.bean.User["User"])

また、@JsonIgnoreProperties を TRUE に設定すると、ユーザー オブジェクト全体が無視され、すべてのフィールドで NULL 値が返されます。

以下のように設定した ObjectMapper についても認識しています。

private static ObjectMapper createCustomObjectMapper() {
        return new ObjectMapper()
                .configure(SerializationFeature.WRAP_ROOT_VALUE, true)
                .configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true)
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                .setAnnotationIntrospector(createJaxbJacksonAnnotationIntrospector());
    }

私はjackson 2.0を使用しています。以下はUser beanのサンプルコードです

**package com.test.webservice.bean;

import javax.persistence.*;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty;

@Entity
@Table(name = "users")
public class User {
    @JsonCreator
    public User(
            @JsonProperty("username") String username,
            @JsonProperty("password") String password,
            @JsonProperty("email") String email,
            @JsonProperty("firstName") String FirstName,
            @JsonProperty("lastName") String LastName,
            @JsonProperty("address") String Address,
            @JsonProperty("city") String City) {
            this.username = username;
            this.password = password;
            this.email = email;
            this.FirstName = FirstName;
            this.LastName = LastName;
            this.Address = Address;
            this.City = City;
        }**

そして、jsonオブジェクトを以下のように設定すると、正常に動作します

{
        "username": "newusername",
        "password": "newpassword",
        "email": "test.test@adrosonic.com",
        "address": "test",
        "firstName": "test",
        "lastName": "test",
        "city": "test"
}

これがどのように機能するのか、またはjson文字列を変更する以外にこの状況で回避策があるかどうかを知りたいです。

お時間をいただきありがとうございます。

4

0 に答える 0