[英]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:它有效,但我补充说:
@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;
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");
}
}
@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();
}
}
@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.