0

我有以下非常简单的 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 类名更改为不同的名称并尝试过,但仍然无法正常工作。顺便说一句,我正在使用 Google App 引擎。我使用的restlet sdk 是Restlet sdk 的GAE 版本。我也尝试过使用 JAX-RS。我解决了库冲突,并且在我第一次尝试时一切正常,如果我更改了字段名称,那么在 JAX-RS 的情况下,它会默默地忽略那些名称已更改的字段。

非常感谢任何猜测或想法或建议。

4

1 回答 1

0

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

于 2013-11-16T14:25:04.540 回答