简体   繁体   English

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

[英]How to send a SOAP object with FEIGN client?

I'm trying to send a SOAP message via a FEIGN client.我正在尝试通过 FEIGN 客户端发送 SOAP 消息。 The problem is that when I send the java object, what is actually being sent is a request with an xml format, instead of a SOAP format.问题是当我发送 java 对象时,实际发送的是 xml 格式的请求,而不是 SOAP 格式。

The client is configured as follows:客户端配置如下:

@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);

}

Looking at the log I see that I am really sending this:查看日志,我发现我真的在发送这个:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Add xmlns="http://tempuri.org/">
    <intA>2</intA>
    <intB>0</intB>
</Add>

When I really should be sending the following message:当我真的应该发送以下消息时:

<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>

Any help is welcome, thanks!欢迎任何帮助,谢谢!

You must define a custom Feign codec to use SOAP, as described in here .你必须定义一个定制的假死编解码器使用SOAP,如在这里

To integrate it with FeignClient, you should define a custom configuration class for it, reference .要将其与 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);

}
@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);
    }
}

It's works, but i had added:它有效,但我补充说:

  1. Create package-info.java into package addRequest/AddResponse将 package-info.java 创建到 package addRequest/AddResponse
    @XmlSchema(
            namespace = "http://tempuri.org/",
            elementFormDefault = XmlNsForm.QUALIFIED,
            xmlns = {
                    @XmlNs(prefix = "xsd", namespaceURI = "http://tempuri.org/")
            }
    )
    package your.package.add;
    
    import javax.xml.bind.annotation.XmlNs;
    import javax.xml.bind.annotation.XmlNsForm;
    import javax.xml.bind.annotation.XmlSchema;

  1. Also, you need to add an interceptor to set content-type and soapAction:此外,您需要添加一个拦截器来设置 content-type 和 soapAction:
    import feign.RequestInterceptor;
    import feign.RequestTemplate;
    
    public class FeignRequestSoapInterceptor implements RequestInterceptor {
    
        @Override
        public void apply(RequestTemplate requestTemplate) {
            requestTemplate.header("Content-Type", "text/xml");
            requestTemplate.header("soapAction", "http://tempuri.org/Add");
        }
    }

  1. In MySoapClientConfiguration, add the FeignRequestSoapInterceptor:在 MySoapClientConfiguration 中,添加 FeignRequestSoapInterceptor:
@Configuration
public class MySoapClientConfiguration {

    private static final JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder()
            .withMarshallerJAXBEncoding("UTF-8")
            .build();

    @Bean
    public Encoder feignEncoder() {
        return new SOAPEncoder(jaxbFactory);
    }

    @Bean
    public Decoder feignDecoder() {
        return new SOAPDecoder(jaxbFactory);
    }

    @Bean
    public FeignRequestSoapInterceptor feignRequestSoapInterceptor() {
        return new FeignRequestSoapInterceptor();
    }
}
  1. FeignClient:假客户端:
    @FeignClient(
      name = "calculatorServer", 
      url = "http://www.dneonline.com/calculator.asmx"
      configuration = MySoapClientConfiguration.class)
    public interface AEMWebServiceFeignClient{
    
        @PostMapping("/Add")
        AddResponse calculate(@RequestBody Add addRequest);
    
    }

I am currently trying this solution to send a SOAP Request via.我目前正在尝试通过此解决方案发送 SOAP 请求。 Feign Clients, however is there a solution for adding data to the "Header"-field? Feign Clients,但是是否有将数据添加到“标题”字段的解决方案? I need to provide some Information in the Header field, but it only puts my data in the body field.我需要在 Header 字段中提供一些信息,但它只将我的数据放在 body 字段中。 It seems I am unable to edit the header field, or do you know some way to do this?似乎我无法编辑标题字段,或者您知道一些方法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM