首页 > 解决方案 > Spring 集成:NotWritablePropertyException - 无效的属性“内容类型”

问题描述

在我的带有 Spring Integration http 出站网关的 Spring Boot 应用程序中,我得到了 http 错误代码 415。所以我headerMapper在配置中添加了一个 bean,但这会抛出NotWritablePropertyException. 消息是属性“内容类型”不可写或具有无效的设置方法。setter 的参数类型是否与 getter 的返回类型匹配?

配置:

<int:gateway id="requestGateway"
    service-interface="org.myorg.springintegration.gateway.http.RequestGateway"
    default-request-channel="post_send_channel" />

<int:channel id="post_send_channel" />

<int:header-enricher input-channel="post_send_channel">
    <int:header name="Content-Type" value="application/json; charset=utf8"/>
</int:header-enricher>

<int-http:outbound-gateway
    request-channel="post_send_channel"
    url="http://localhost:8080/incomes" http-method="POST"
    expected-response-type="java.lang.String" />

请求网关

public interface RequestGateway {
    String echo(Map<String, String> request);
}

主班

@SpringBootApplication
public class HttpApplication {

    public static void main(String[] args) {
        SpringApplication.run(HttpApplication.class, args);

        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("http-outbound-gateway.xml");

        Map<String, String> postMap = new HashMap<String, String>();
        postMap.put("amount", "2000");
        postMap.put("description", "Second Income");

        RequestGateway rg = context.getBean("requestGateway", RequestGateway.class);
        System.out.println(rg.echo(postMap));

        context.close();
    }
}

2018 年 5 月 17 日更新header-enricher按照 Artem Bilan 的建议配置 a 后,我现在收到以下错误:

2018-05-17 12:30:48.354  INFO 4452 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started _org.springframework.integration.errorLogger
[WARNING]
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:496)
    at java.lang.Thread.run (Thread.java:748)
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.HashMap<?, ?>] to type [java.lang.String]
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound (GenericConversionService.java:321)
    at org.springframework.core.convert.support.GenericConversionService.convert (GenericConversionService.java:194)
    at org.springframework.core.convert.support.GenericConversionService.convert (GenericConversionService.java:174)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.convert (GatewayProxyFactoryBean.java:755)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.invokeGatewayMethod (GatewayProxyFactoryBean.java:527)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.doInvoke (GatewayProxyFactoryBean.java:469)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.invoke (GatewayProxyFactoryBean.java:460)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke (JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy79.echo (Unknown Source)
    at org.javacodegeeks.springintegration.gateway.http.HttpApplication.main (HttpApplication.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:496)
    at java.lang.Thread.run (Thread.java:748)

标签: spring-bootspring-integration

解决方案


我猜你的意思是header-enricher以前http:Outbound-Gateway。您需要填充这样的标头,同时映射器即将向请求添加现有标头。

有关更多信息,请参阅参考手册:https ://docs.spring.io/spring-integration/docs/5.0.4.RELEASE/reference/html/messaging-transformation-chapter.html#content-enricher


推荐阅读