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,以下是用户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