无法识别的字段"用户" (类com.test.webservice.bean.User),未标记为可忽略

时间:2015-03-16 13:52:42

标签: java json jersey jackson jackson-modules

您好我试图通过泽西服务转换下面的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字符串。

谢谢你的时间。

0 个答案:

没有答案