【发布时间】:2021-12-18 07:24:29
【问题描述】:
我尝试使用的 WebService 是 SOAP,它只接收 HTTP 方法 POST 的请求,但由于某种原因,我无法做到这一点,它只发送 GET。
我一直在松散地遵循本指南:https://spring.io/guides/gs/consuming-web-service/
我认为这不会是错误方法的情况,但我已经尝试通过 Postman 使用记录的信封,它工作正常,也是这样:
这就是我的做法:
package com.example.ws;
import com.demo.partner.wsdl.ObjectFactory;
import com.demo.partner.wsdl.StoreInformation;
import com.example.util.PartnerWSHttpHeaderCallBack;
import com.example.util.SoapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
public class StoreInformationClient extends WebServiceGatewaySupport {
private static final Logger log = LoggerFactory.getLogger(StoreInformationClient.class);
@Autowired
private ObjectFactory objectFactory;
public StoreInformation getStoreInformation() {
StoreInformation storeInformation = objectFactory.createStoreInformation();
storeInformation.setStoreID("99633");
return (StoreInformation) getWebServiceTemplate()
.marshalSendAndReceive("https://aurl.com/api/ws.wsdl", SoapUtils.buildEnvelope(storeInformation, StoreInformation.class),
new PartnerWSHttpHeaderCallBack());
}
}
package com.example.configuration;
import com.demo.partner.wsdl.ObjectFactory;
import com.example.ws.StoreInformationClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
@Configuration
public class AppConfiguration {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.demo.partner.wsdl");
return marshaller;
}
@Bean
public StoreInformationClient countryClient(Jaxb2Marshaller marshaller) {
StoreInformationClient client = new StoreInformationClient();
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
@Bean
public ObjectFactory objectFactory() {
return new ObjectFactory();
}
}
package com.example.util;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.WebServiceMessageCallback;
import org.springframework.ws.transport.WebServiceConnection;
import org.springframework.ws.transport.context.TransportContext;
import org.springframework.ws.transport.context.TransportContextHolder;
import org.springframework.ws.transport.http.HttpUrlConnection;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class PartnerWSHttpHeaderCallBack implements WebServiceMessageCallback {
@Override
public void doWithMessage(WebServiceMessage webServiceMessage) {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Basic definitelyABase64value");
headers.put("SOAPAction", "http://www.partner.com/action/GetStoreInformation");
headers.put("Content-Type", "text/xml");
addRequestHeader(headers);
}
private void addRequestHeader(Map<String, String> headers) {
TransportContext context = TransportContextHolder.getTransportContext();
WebServiceConnection connection = context.getConnection();
if (connection instanceof HttpUrlConnection) {
HttpUrlConnection conn = (HttpUrlConnection) connection;
for (Map.Entry<String, String> entry : headers.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
try {
conn.addRequestHeader(k, v);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
【问题讨论】:
-
你怎么知道发送的是get而不是post?你有什么错误吗?
-
我得到与通过 Postman 执行相同的错误,并且您可以看到连接正在使用 GET 方法的图像。我已经尝试在 PartnerWSHttpHeaderCallBack 中设置它,但它也不起作用。
标签: java spring-boot web-services soap http-method