dropwizard-giving-400-error-when-creating-new-resource使用POST

时间:2015-06-30 05:29:47

标签: java json rest post dropwizard

public class UserProfileData {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "firstName", nullable = false)
    private String firstName;

    @Column(name = "lastName", nullable = true)
    private String lastName;

    @Column(name = "country", nullable = true)
    private String country;

    public UserProfileData() {
    }

    public UserProfileData(String firstName, String lastName, String country) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.country = country;
    }


    public long getId() {
        return id;
    }


    public void setObjectId(long id) {
        this.id = id;
    }

    @JsonProperty
    public String getFirstName() {
        return firstName;
    }

    @JsonProperty
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @JsonProperty
    public String getLastName() {
        return lastName;
    }

    @JsonProperty
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @JsonProperty
    public String getCountry() {
        return country;
    }

    @JsonProperty
    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof UserProfileData)) return false;

        final UserProfileData that = (UserProfileData) o;

        return Objects.equals(this.id, that.id) &&
                Objects.equals(this.firstName, that.firstName) &&
                Objects.equals(this.lastName, that.lastName) &&
                Objects.equals(this.country, that.country);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, firstName, lastName, country);
    }
}

========================================================================

DAO Method

   public UserProfileData create(UserProfileData userProfileData) {
        LOGGER.info("UserProfileData: Persisting New User");
        return persist(userProfileData);        
    }

=======================================================================

Resource Call
@Path("/user")
@Produces(MediaType.APPLICATION_JSON)
public class UserProfileDataResource {

 @POST
    @Path("/create")
    @Consumes(MediaType.APPLICATION_JSON)
    @UnitOfWork
    public UserProfileData createUser(UserProfileData userProfileData) {
        //ResourceHelper.checkRequiredParams(requesterId);
        LOGGER.info("User created");
        return userProfileDataDAO.create(userProfileData);
    }
======================================================================

当我使用创建新用户数据来调用资源时,它会给我错误

  

curl -H“Content-Type:application / json”-X POST -d   “{” 名字 “:” 萨钦 “ ”姓氏“: ”德家“, ”国“: ”印度“}”   http://localhost:8080/user/create

     

错误:{“code”:400,“message”:“无法处理JSON”,“details”:null}

尝试几乎所有事情,但无法弄清楚为什么会出现这个错误以及如何解决它?

4 个答案:

答案 0 :(得分:1)

要获取有关400错误的有用消息,请在球衣上注册:

environment.jersey().register(new JsonProcessingExceptionMapper(true));

它将在400响应中给出更详细的消息。

答案 1 :(得分:0)

你的curl请求用引号("")封装JSON数据,因为JSON中的引号而中断。所以你发送到服务器的实际上是" {"。

使用撇号(')代替:

curl -H "Content-Type: application/json" -X POST -d '{"firstName":"Sachin","lastName":"Tendulkar","country":"India"}' http://localhost:8080/user/create

答案 2 :(得分:0)

您可以尝试使用其他类型的帖子数据,使用raw(使用JSON格式字符串)代替form-datax-www-form-urlencoded。按照这个例子,我使用Postman客户端&原始的帖子数据,它的工作! enter image description here

答案 3 :(得分:0)

可能有几个原因导致此错误。尝试将日志记录级别调整到配置文件中,即:

logging:
    level: DEBUG