使用JERSEY和JACKSON读入POST请求数据

时间:2014-08-26 02:49:21

标签: java json jersey jackson dropwizard

我收到以下错误:

  

无法反序列化java.util.ArrayList的实例   VALUE_STRING标记

当我在本地发送以下卷曲请求时:

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

这是我的资源和表示类。我正在做的就是尝试接收POST请求和toString()信息,以便我可以验证Jackson正确读取它并构建正确的POJO。我错过了其他注释吗?

@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();
    }

和表示类:

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

1 个答案:

答案 0 :(得分:1)

听起来杰克逊不知道如何将参数分配给构造函数。变化

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

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