Simple Java API - Error: Unrecognized field, not marked as ignorable

HTTP 418 :

I will preface this question by saying, that I don't use Jackson - and all questions I have found regarding this problem seems to have something to do with Jackson.

I have a small/simple JAX-RS API service, but I receive an error on a specific method call. First, I will explain what my program consists of.

Domain classes

I have two classes, Request and Answer. The hierarchy is that users can make a request, and then other users can answer that request. Therefore, Request.java conatins an ArrayList of answers, so that answers can be added to a specific request.

Request.java (pay special attention to the ArrayList answers)

public class Request {
    private int id;
    private String originalText;
    public ArrayList<Answer> answers = new ArrayList<Answer>(); //pay attention

    //getters and setters - removed for brevity
}

Answer.java

public class Answer {
    private int requestId;
    private String newText;

    //getters and setters - removed for brevity
}

Endpoint

The endpoint consists of my HTTP requests. It uses in-memory storage. I have another POST method that works for posting a request to the in-memory db, but for brevity I have not posted it.

The POST method I have problem with below, createAnswer, tries to save an answer to an existing request.

Endpoint.java

public class Endpoint {

    private Map<Integer, Request> inMemoryDb = new ConcurrentHashMap<Integer, Request>();
    private AtomicInteger idCounter = new AtomicInteger();

    @POST
    @Path("/answer/{id}")
    @Consumes({MediaType.APPLICATION_JSON})
    public Response createAnswer(Answer answer, @PathParam("id") int id) {
        final Request request = inMemoryDb.get(id);
        if (request == null) {
            return Response.status(404).entity("Request not found").build();
        }
        else {
            request.answers.add(answer);
            return Response.ok().build();
        }
    }
}

As you can see, I try to add an answer to an existing request within the if-statement: request.answers.add(answer);

I make the POST request with Postman using JSON:

{
    "requestId": 1,
    "newText": "Hello World"
}

This is the error I receive:

  • Unrecognized field "newText" (class dk.domain.Answer), not marked as ignorable (2 known properties: "id", "originalText"]) at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 2, column: 21] (through reference chain: dk.domain.Answer["newText"])

As can be seen in the error message, it says that the error is due to two fields in Request.java "not marked as ignorable"?

All posts about this error that I have found have been due to some Jackson setup, but I don't use Jackson, so I can't use the answers in there.

I suspect that there is something wrong about how I try to add an answer to the in-memory database (maybe due to the hiearchy of my two domain classes, since the answer-array of an existing request is empty before trying to add the first answer?), but I am not sure.

Hope anybody can help, will be greatly appreciated

Extra info

I have tried to add breakpoints to the method, but it seems like it doesn't hit the method. This is not due to wrong path/URL or similar, but maybe there is something wrong with the signature. I will keep trying.

HTTP 418 :

The problem was my getters and setters in Answer.java, which I had not included in the OP.

I had copied the getters and setters from my Request.java class and not changed the naming, so my Answer.java class looked like this (I have marked important lines with comment "PAY ATTENTION"):

public class Answer {
    private int requestId;
    private String newText; //variable is called newText, but getters and setters are named after a variable in Request.java, originalText

    public int getRequestId() {
        return requestId;
    }

    public void setRequestId(int requestId) {
        this.requestId = requestId;
    }

    public String getOriginalText() { //PAY ATTENTION TO NAME OF GETTER
        return newText;
    }

    public void setOriginalText(String newText) { //PAY ATTENTION TO NAME OF SETTER
        this.newText = newText;
    }
}

As you can see, I copied the names of my getters and setters from Request.java and didn't change the naming, so it was called getOriginalText and setOriginalText instead of getNewText and setNewText, which would have been correct. Apparently, no matter what you have called your private variable, Java will retrieve the name from your getters and setters, and therefore believed that my variable in Answer.java was named originalText instead of newText.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=166416&siteId=1