在Spring Boot中,您可以使用@Bean
注释来配置bean。您可以为不同的bean使用配置类。在这些课程中,您需要@Configuaration
注释。
这个tutorial描述了Spring教程的“第二部分”。提供教程的主要事情是:(基于Spring教程)
问题
的SOAP Web服务我消耗需要基本的HTTP认证,所以我 需要认证头添加到请求。
没有认证
首先你需要在spring.io教程已经实现了一个请求,没有 认证等等。然后我将 用认证头修改http请求。
获取自定义HTTP请求WebServiceMessageSender
原始的HTTP连接处于WeatherConfiguration 类访问。在天气客户端中,您可以在 WebServiceTemplate中设置消息发送者。消息发件人可以访问原始http 连接。因此,现在是扩展 HttpUrlConnectionMessageSender并编写自定义实现 的时候了,它会将验证头添加到请求中。我的自定义 发件人如下:
public class WebServiceMessageSenderWithAuth extends HttpUrlConnectionMessageSender{
@Override
protected void prepareConnection(HttpURLConnection connection)
throws IOException {
BASE64Encoder enc = new sun.misc.BASE64Encoder();
String userpassword = "yourLogin:yourPassword";
String encodedAuthorization = enc.encode(userpassword.getBytes());
connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
super.prepareConnection(connection);
}
@Bean
public WeatherClient weatherClient(Jaxb2Marshaller marshaller){
WebServiceTemplate template = client.getWebServiceTemplate();
template.setMessageSender(new WebServiceMessageSenderWithAuth());
return client;
}
您可以随时导入XML与'@ImportResource( “applicationContext.xml中”)' – varren