You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Integration HTTP Outbound Gateway PUT请求报400无请求体求助

问题排查与解决方案

针对Spring Integration HTTP Outbound Gateway发起PUT请求时出现400 Bad Request(提示无请求体)的问题,可按以下步骤排查解决:

1. 修复Header Enricher的语法错误

原配置中int:header-enricher缺少尖括号,会导致配置解析异常,直接修正为:

<int:header-enricher>
    <int:header name="Content-Type" value="application/xml"/>
    <int:header name="Accept" value="application/json"/>
</int:header-enricher>

2. 配置XML序列化转换器

Spring Integration的HTTP Outbound Gateway依赖RestTemplate处理请求体序列化,若未配置JAXB相关转换器,无法将SampleRequest对象转为XML格式请求体。需添加以下配置:

<!-- JAXB Marshaller,绑定需要序列化的实体类 -->
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.test.integration.request.SampleRequest</value>
            <value>com.test.integration.request.User</value> <!-- 必须包含User类 -->
        </list>
    </property>
</bean>

<!-- 自定义RestTemplate,添加XML序列化转换器 -->
<bean id="customRestTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
        <list>
            <!-- XML转换器 -->
            <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                <constructor-arg ref="jaxbMarshaller"/>
            </bean>
            <!-- 保留字符串和JSON转换器以兼容其他场景 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </list>
    </property>
</bean>

然后在HTTP Outbound Gateway中指定使用该RestTemplate:

<int-http:outbound-gateway
    id="testOutboundGateway"
    url-expression="headers.testUrl"
    http-method="PUT"
    request-factory="testRequestFactory"
    extract-request-payload="true"
    charset="UTF-8"
    mapped-request-headers="Content-Type,Accept"
    expected-response-type="com.test.integration.TestResponse"
    rest-template="customRestTemplate"> <!-- 添加这行 -->
</int-http:outbound-gateway>

3. 验证实体类JAXB注解完整性

确保User类也配置了正确的JAXB注解,否则JAXB无法完整序列化对象结构:

@XmlRootElement(name = "User")
public class User implements Serializable {
    // 字段示例
    private String userId;

    @XmlElement(name = "UserId")
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }
}

4. 开启日志排查细节

添加DEBUG级别的日志配置,查看请求发送的具体内容和序列化过程:

<!-- logback示例配置 -->
<logger name="org.springframework.integration" level="DEBUG"/>
<logger name="org.springframework.web.client.RestTemplate" level="DEBUG"/>

通过日志可以确认请求体是否被正确生成,以及是否存在序列化异常。


内容的提问来源于stack exchange,提问作者KeepItSimple

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 03:46:08