【问题标题】:dropwizard-giving-400-error-when-creating-new-resource using POSTdropwizard-giving-400-error-when-creating-new-resource 使用 POST
【发布时间】:2015-06-30 05:29:47
【问题描述】:
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 "{"firstName":"Sachin","lastName":"Tendulkar","country":"India"}" http://localhost:8080/user/create

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

尝试了几乎所有方法,但无法弄清楚为什么会出现此错误以及如何解决?

【问题讨论】:

    标签: java json rest post dropwizard


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      您收到此错误的原因可能有多种。尝试将日志记录级别放在配置文件中进行调试,即:

      logging:
          level: DEBUG
      

      【讨论】:

        【解决方案3】:

        您的 curl 请求使用引号 ("") 封装 JSON 数据,由于 JSON 中的引号而中断。所以你发送给服务器的实际上是“{”。

        改用撇号 ('):

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

        【讨论】:

        • 我尝试使用 (') 但仍然得到相同的错误 curl -H "Content-Type: application/json" -X POST -d '{"firstName":"Sachin","lastName" :"Tendulkar","country":"India"}' localhost:8080/user/create
        【解决方案4】:

        您能否尝试使用另一种类型的帖子数据,使用 raw(带有 JSON 格式字符串)而不是 form-datax-www-form-urlencoded。按照那个例子,我使用了 Postman 客户端和原始帖子数据,它起作用了!

        【讨论】:

          猜你喜欢
          • 2019-10-22
          • 1970-01-01
          • 1970-01-01
          • 2022-12-02
          • 2022-12-27
          • 1970-01-01
          • 2022-12-31
          • 2020-04-17
          • 1970-01-01
          相关资源
          最近更新 更多