質問

I have the following very simple POJO object

Working Version

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 request body that i used

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

Modified Version which is resulting in Unrecognized Field

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;
    }
}

After changing the first name and last name to firstEmpName and lastEmpName, i tried to post the below request body

JSON request body that i used

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

Exception i got

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)

Rest of the sample application is as follows:

@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;

        }
    }

Everytime i change a field name, do i need to do something for that field to be recognized.

I also tried using Jax-rs instead of Restlet, i still see the same problem.

So to summarize the problem that i am having, whatever the pojo that i create in the first time is what remains and is working. If i changing any fields in the POJO like that of above, for example from firstName to firstEmpName, then i get the above exception. I tried to clean the classes, restarted the server. Removed local_db.bin and even changed the Pojo class name to a different and tried but still it is not working. BTW, i am using Google App engine. The restlet sdk i used was the GAE edition of Restlet sdk. Also i tried with JAX-RS. I resolved the library conflicts and everything works the first time i try and if i change the field names, then in the case JAX-RS, it silently ignores those fields whose names have been changed.

Any guess or thoughts or suggestions is much appreciated.

役に立ちましたか?

解決

Properties in Java Beans are defined by the method names, not the field names. So you should rename the get and set methods to match the field names.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top