如何使用 FEIGN 客户端发送 SOAP 对象?

Álv*_*aro 1 java spring soap spring-boot feign

我正在尝试通过 FEIGN 客户端发送 SOAP 消息。问题是当我发送 java 对象时,实际发送的是一个 xml 格式的请求,而不是一个 SOAP 格式。

客户端配置如下:

@FeignClient(name = "calculatorServer", url = "http://www.dneonline.com/calculator.asmx")
public interface AEMWebServiceFeignClient{

    @PostMapping(value = "", consumes = MediaType.TEXT_XML, produces = MediaType.TEXT_XML)
    AddResponse calculate(@RequestBody Add addRequest);

}
Run Code Online (Sandbox Code Playgroud)

查看日志,我看到我真的在发送这个:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Add xmlns="http://tempuri.org/">
    <intA>2</intA>
    <intB>0</intB>
</Add>
Run Code Online (Sandbox Code Playgroud)

当我真的应该发送以下消息时:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:Add>
         <tem:intA>2</tem:intA>
         <tem:intB>0</tem:intB>
      </tem:Add>
   </soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)

欢迎任何帮助,谢谢!

Adh*_*ita 8

你必须定义一个定制的假死编解码器使用SOAP,如在这里

要将其与 FeignClient 集成,您应该为其定义一个自定义配置类,参考.

@FeignClient(
  name = "calculatorServer", 
  url = "http://www.dneonline.com/calculator.asmx"
  configuration = MySoapClientConfiguration.class)
public interface AEMWebServiceFeignClient{

    @PostMapping(value = "", consumes = MediaType.TEXT_XML, produces = MediaType.TEXT_XML)
    AddResponse calculate(@RequestBody Add addRequest);

}
Run Code Online (Sandbox Code Playgroud)
@Configuration
public class MySoapClientConfiguration {

    private static final JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder()
       .withMarshallerJAXBEncoding("UTF-8")
       .withMarshallerSchemaLocation("http://apihost http://apihost/schema.xsd")
       .build();

    @Bean
    public Encoder feignEncoder() {
        return new SOAPEncoder(jaxbFactory);
    }
    @Bean
    public Decoder feignDecoder() {
        return new SOAPDecoder(jaxbFactory);
    }
}
Run Code Online (Sandbox Code Playgroud)