修改数据对象中的字段会导致无法识别的字段异常



我有以下非常简单的POJO对象

工作版本

package com.example.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement 
public class Employee {
    long id;
    String firstName;
    String lastName;
    public long getId() {
        return id;
    }
    public void setId(long l) {
        this.id = l;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

我使用的 JSON 请求正文

{"id":"23", "firstName":"John", "lastName":"Smith"}

修改后的版本导致无法识别的字段

package com.example.model;

public class Employee {
    long id;
    **String firstEmpName;**  // Resulting in unrecognized Field
    **String lastEmpName;**   // Resulting in Unrecognized Field
    public long getId() {
        return id;
    }
    public void setId(long l) {
        this.id = l;
    }
    public String getFirstName() {
        return firstEmpName;
    }
    public void setFirstName(String firstName) {
        this.firstEmpName = firstName;
    }
    public String getLastName() {
        return lastEmpName;
    }
    public void setLastName(String lastName) {
        this.lastEmpName = lastName;
    }
}

将名字和姓氏更改为 firstEmpName 和 lastEmpName 后,我尝试发布以下请求正文

我使用的 JSON 请求正文

{"id":"23", "firstEmpName":"John", "lastEmpName":"Smith"}

我得到的例外

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "firstEmpName" (Class com.example.model.Employee), not marked as ignorable
 at [Source: org.restlet.engine.io.UnclosableInputStream@2275a30f; line: 1, column: 29] (through reference chain: com.example.model.Employee["firstEmpName"])
    at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
    at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)

示例应用程序的其余部分如下所示:

@Override
public Restlet createInboundRoot() {
// Create a router Restlet that routes each call to a
    // new instance of HelloWorldResource.
    Router router = new Router(getContext());
    // Defines only one route
    router.attach("/employees/{id}", SampleAppRestlet_ServerServlet.class);

    return router;
}


      public class SampleAppRestlet_ServerServlet extends ServerResource {
         @Get
        public Employee getEmployee(){
                Employee m = new Employee();
                m.setFirstName("John");
                m.setLastName("Smith");
                m.setId(23);
                return m;
        }
    }

每次更改字段名称时,我是否需要执行某些操作才能识别该字段。

我也尝试使用 Jax-rs 而不是 Restlet,我仍然看到同样的问题。

因此,总结一下我遇到的问题,无论我第一次创建的pojo是什么,都是仍然存在并且正在起作用的。如果我像上面一样更改 POJO 中的任何字段,例如从 firstName 更改为 firstEmpName,那么我会得到上述异常。我尝试清理类,重新启动服务器。删除了local_db.bin,甚至将 Pojo 类名称更改为不同的并尝试过,但仍然不起作用。顺便说一句,我正在使用谷歌应用引擎。我使用的 restlet sdk 是 Restlet sdk 的 GAE 版本。我也尝试了JAX-RS。我解决了库冲突,第一次尝试时一切正常,如果我更改字段名称,那么在 JAX-RS 的情况下,它会默默忽略那些名称已更改的字段。

任何猜测或想法或建议将不胜感激。

Java

Bean 中的属性由方法名称定义,而不是字段名称。因此,您应该重命名getset方法以匹配字段名称。

最新更新