Java 制作弹簧&x27;s@Service和@Configuration类是否更通用?
其思想是使用某种(自定义)设计模式(或任何其他Java构造,如参数化类型等),使Java 制作弹簧&x27;s@Service和@Configuration类是否更通用?,java,spring,generics,design-patterns,Java,Spring,Generics,Design Patterns,其思想是使用某种(自定义)设计模式(或任何其他Java构造,如参数化类型等),使@Configuration和@Service类更加通用 问题是: 我的Spring Boot应用程序的一部分就像一个客户端,也就是说,它使用一些SOAP Web服务。我正使用七个(将来可能更多)服务。我用来发送请求和接收响应的类是由基于WSDL的Maven JAXB2插件生成的。在本文中,我将概述最简单的服务: HelloWorld 控制器层: @Autowired private HelloWorldClient
@Configuration
和@Service
类更加通用
问题是:
我的Spring Boot应用程序的一部分就像一个客户端,也就是说,它使用一些SOAP Web服务。我正使用七个(将来可能更多)服务。我用来发送请求和接收响应的类是由基于WSDL的Maven JAXB2插件生成的。在本文中,我将概述最简单的服务:
HelloWorld
控制器层:
@Autowired
private HelloWorldClient helloWorldClient;
@RequestMapping(value = "/helloWorld", method = RequestMethod.POST)
@ResponseBody
public HelloWorldResponse helloWorldRequest(@RequestParam(value = "myValue") String myValue) {
HelloWorldResponse response = helloWorldClient.getValue(myValue);
return response;
}
服务类的配置
@Configuration
public class HelloWorldConfiguration {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.zesium.arvato.model"); //this is package where I placed generated classes from wsdl scheme
return marshaller;
}
@Bean
public HelloWorldClient helloWorldClient(Jaxb2Marshaller marshaller) {
HelloWorldClient client = new HelloWorldClient();
client.setDefaultUri("");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}
@Service
public class HelloWorldClient extends WebServiceGatewaySupport {
@Autowired
private ObjectFactory objectFactory;
public HelloWorldResponse getValue(String myValue) {
HelloWorld request = objectFactory.createHelloWorld();
JAXBElement<String> value = objectFactory.createHelloWorldMyValue(myValue);
request.setMyValue(value);
HelloWorldResponse response = (HelloWorldResponse) getWebServiceTemplate()
.marshalSendAndReceive("https://clienttesthorizon.horizonafs.com/AFSServices/AFSService.svc/basicHttpBinding",
request, new SoapActionCallback("http://tempuri.org/IAFSService/HelloWorld"));
return response;
}
}
服务等级
@Configuration
public class HelloWorldConfiguration {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.zesium.arvato.model"); //this is package where I placed generated classes from wsdl scheme
return marshaller;
}
@Bean
public HelloWorldClient helloWorldClient(Jaxb2Marshaller marshaller) {
HelloWorldClient client = new HelloWorldClient();
client.setDefaultUri("");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}
@Service
public class HelloWorldClient extends WebServiceGatewaySupport {
@Autowired
private ObjectFactory objectFactory;
public HelloWorldResponse getValue(String myValue) {
HelloWorld request = objectFactory.createHelloWorld();
JAXBElement<String> value = objectFactory.createHelloWorldMyValue(myValue);
request.setMyValue(value);
HelloWorldResponse response = (HelloWorldResponse) getWebServiceTemplate()
.marshalSendAndReceive("https://clienttesthorizon.horizonafs.com/AFSServices/AFSService.svc/basicHttpBinding",
request, new SoapActionCallback("http://tempuri.org/IAFSService/HelloWorld"));
return response;
}
}
}
我知道这是错误的,但我不知道如何实现这一点,我已经做了一年半的后端在这一点上。我知道这是一个非常宽泛的问题,但如果有人能给我一些建议去哪里寻找,也许是一个特定的设计模式或类似的东西,那就太好了