【问题标题】:Http Bad Request - The request sent by the client was syntactically incorrectHttp Bad Request - 客户端发送的请求在语法上不正确
【发布时间】:2016-12-08 11:05:23
【问题描述】:

我对这个 API 的 POST 调用给了我一个 400。即使序列化的 DTO 对象是正确的,它也会给出一个语法错误。请求正文使用了与类中的字段名相对应的正确字段名。

此外,ApI 甚至没有受到打击。

在标题中,内容类型:application/json

我的 API 是:

@RequestMapping(value = "/surveymonkey/webhook/receiver", method = RequestMethod.POST)
    @ResponseBody
    public void respondToSurveyMonkeyPOSTCall(@RequestBody NPSWebhookRequestBody npsWebhookRequestBody, HttpServletRequest request) {


        String objectType = null;

        String objectId =null;

        if(npsWebhookRequestBody!=null){

            objectType = npsWebhookRequestBody.getObjectType();

            objectId = npsWebhookRequestBody.getObjectId();
        }

        service.getCall(objectType,objectid
    }

我的被序列化为 requestbody 的 DTO 类是:

package com.lk.scheduler.beans;

import com.google.gson.annotations.SerializedName;

public class NPSWebhookRequestBody {

    @SerializedName("name")
    String name;

    @SerializedName("event_id")
    String eventId;

    @SerializedName("object_type")
    String objectType;

    @SerializedName("object_id")
    String objectId;

    @SerializedName("event_type")
    String eventType;

    @SerializedName("event_datetime")
    String eventDatetime;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the eventId
     */
    public String getEventId() {
        return eventId;
    }

    /**
     * @param eventId the eventId to set
     */
    public void setEventId(String eventId) {
        this.eventId = eventId;
    }

    /**
     * @return the objectType
     */
    public String getObjectType() {
        return objectType;
    }

    /**
     * @param objectType the objectType to set
     */
    public void setObjectType(String objectType) {
        this.objectType = objectType;
    }

    /**
     * @return the objectId
     */
    public String getObjectId() {
        return objectId;
    }

    /**
     * @param objectId the objectId to set
     */
    public void setObjectId(String objectId) {
        this.objectId = objectId;
    }

    /**
     * @return the eventType
     */
    public String getEventType() {
        return eventType;
    }

    /**
     * @param eventType the eventType to set
     */
    public void setEventType(String eventType) {
        this.eventType = eventType;
    }

    /**
     * @return the eventDatetime
     */
    public String getEventDatetime() {
        return eventDatetime;
    }

    /**
     * @param eventDatetime the eventDatetime to set
     */
    public void setEventDatetime(String eventDatetime) {
        this.eventDatetime = eventDatetime;
    }



}

邮递员打电话给 400 :

附: : 对该 API 的 HttpGET 和 HttpHEAD 调用正常。

【问题讨论】:

  • 您的应用程序的日志文件显示了什么?
  • 您是否尝试将其作为表单数据发送?

标签: java spring api post httprequest


【解决方案1】:

我很幸运地发现了这个错误。显然 Spring 的 @RequestBody 注释不适用于使用 Google 的 gson 库进行序列化反序列化。

@SerializedName(这是 Gson 的注释属性)在反序列化时将不起作用,因此无法解析 400 错误请求作为请求正文..

我们可以使用fastxml-jackson的@JsonProperty。它工作正常,API 给出 ​​200 响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    相关资源
    最近更新 更多