srirachablender
srirachablender

Reputation: 89

Using JERSEY and JACKSON to read in POST request data

I'm getting an error of the following:

Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token

when I send the following curl request locally:

> curl -H "Content-Type: application/json" -d
> '{"url":"http://www.facebook.com", "whitelist":"[white,list]",
> "blacklist":"[black,list]"}' http://localhost:8080/

Here's my resource and representation class. All I'm doing is trying to taking in the POST request and toString()'ing the information so I can validate that its being read correct by Jackson and building the proper POJO. Am I missing other annotations?

@Path("/")
public class ValidationRequestResource {

    @GET
    @Produces(value = MediaType.TEXT_PLAIN)
    public Response index() {
        System.out.println("calling index() method!");
        return Response.status(200).build();
    }

    @POST
    @Produces(value = MediaType.TEXT_PLAIN)
    @Consumes(value = MediaType.APPLICATION_JSON)
    public Response processValidationRequest(ValidationRequest validationRequest) {
        System.out.println("calling processValidationRequest() method");
        String output = validationRequest.toString();
        return Response.status(200).entity(output).build();
    }

and representation class:

package validation;

import java.util.ArrayList;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class ValidationRequest {

    private String url;
    private ArrayList<String> whitelist;
    private ArrayList<String> blacklist;

    @JsonCreator
public ValidationRequest(@JsonProperty("url") String url, @JsonProperty("whitelist") ArrayList<String> whitelist, @JsonProperty("blacklist") ArrayList<String> blacklist) {
        this.url = url;
        this.whitelist = whitelist;
        this.blacklist = blacklist;
    }

    @JsonProperty
    public String getUrl() {
        return this.url;
    }

    @JsonProperty
    public void setUrl(String url) {
        this.url = url;
    }

    @JsonProperty
    public ArrayList<String> getWhitelist() {
        return this.whitelist;
    }

    @JsonProperty
    public void setWhitelist(ArrayList<String> whitelist) {
        this.whitelist = whitelist;
    }

    @JsonProperty
    public ArrayList<String> getBlacklist() {
        return this.blacklist;
    }

    @JsonProperty
    public void setBlacklist(ArrayList<String> blacklist) {
        this.blacklist = blacklist;
    }

    @Override
    public String toString() {
        return new StringBuffer("URL: ").append(this.getUrl())
                   .append(" Whitelist: ").append(this.getWhitelist())
                   .append(" Blacklist: ").append(this.getBlacklist())
                   .toString();
    }
}

Upvotes: 1

Views: 1337

Answers (1)

user1596371
user1596371

Reputation:

Sounds like Jackson doesn't know how to assign the parameters to the constructor. Change

public ValidationRequest(String url, ArrayList<String> whitelist, ArrayList<String> blacklist) {

to

public ValidationRequest(@JsonProperty("url") String url, @JsonProperty("whitelist") ArrayList<String> whitelist, @JsonProperty("blacklist") ArrayList<String> blacklist) {

Upvotes: 1

Related Questions