Spring-ws javaconfig位置转换
Spring-ws javaconfig location transformation
我正在尝试使用 Java-Config 设置 Web 服务项目(目前是 SOAP - 但最终会添加 REST)。不幸的是,我无法自动公开 WSDL(据推测,Spring 可以根据 XSD 生成它并公开它)。
我找到的唯一文档在定义 servlet (web.xml) 时使用了 xml 配置。
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
如何使用 Java-config 完成此操作?
WebApplicationInitializer
public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* {@inheritDoc}
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {WebSecurityConfiguration.class};
}
/**
* {@inheritDoc}
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {WebServicesConfiguration.class};
}
/**
* {@inheritDoc}
*/
@Override
protected String[] getServletMappings() {
// get all mappings
return new String[] { "/" };
}
Web 服务配置
@EnableWs
@Configuration
@ComponentScan("com.questsoftware")
public class WebServicesConfiguration extends WsConfigurationSupport {
@Bean
public XsdSchema schema() {
return new SimpleXsdSchema(new ClassPathResource("lookup.xsd"));
}
@Bean
public DefaultWsdl11Definition defaultWsdl11Definition() {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setTargetNamespace("http://com/[mycompany]/Lookup");
wsdl11Definition.setPortTypeName("Lookup");
wsdl11Definition.setLocationUri("/Lookup");
wsdl11Definition.setSchema(schema());
return wsdl11Definition;
}
需要说明的是 - 我还有一个 WebApplicationConfiguration (REST) - 但我还没有将它添加到 WebApplicationInitializer 中(运行 先解决这个问题)。
WebApplicationConfiguration
@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Configuration
@ComponentScan("com.mycompany")
public class WebApplicationConfiguration extends WebMvcConfigurerAdapter {
/**
* Defines {@link JdbcTemplate} as a Spring managed bean.
*
* @return
*/
@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
public DataSource dataSource() {
...
return dataSource;
}
/**
* Defines a {@link ViewResolver} as a Spring managed bean.
*
* @return the viewResolver
*/
@Bean
public ViewResolver viewResolver() {
final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages");
resolver.setSuffix(".jsp");
return resolver;
}
/**
* Registers resource handlers with Spring.
*
* @param registry the {@link ResourceHandlerRegistry}
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/view/**").addResourceLocations("/view/");
}
参考
http://docs.spring.io/spring-ws/sites/2.0/reference/html/server.html#server-automatic-wsdl-exposure
我想出了一个解决方案,但我不完全确定它是 'correct' 还是 'preferred' 方式。
我遵循了教程并简单地在基于 java-config 的 WebApplicationConfiguration 中定义了 beans,这些 bean 在基于教程 xml 的配置中定义。 --我的理解是,真的没有根本的区别,你只需要将 bean 添加到 Spring 容器中,所以 Spring 可以发挥它的魔力。
我对上面的代码进行了以下更改:
WebApplicationConfiguration
添加了以下bean
/**
* Defines the {@link SaajSoapMessageFactory} as a Spring managed bean.
*
* Note: we need a Soap implementation of a MessageFactory to create soap messages in Spring-WS.
*
* @return the messageFactory
*/
@Bean
public SaajSoapMessageFactory messageFactory() {
return new SaajSoapMessageFactory();
}
/**
* Defines the {@link WebServiceMessageReceiverHandlerAdapter} as a Spring managed bean.
*
* Note: We need to add this bean to the context in order for the DispatcherServlet to delegate to a MessageDispatcher
* (as opposed to Controllers). The MessageDispatcher is necessary for Spring-WS.
*
* @return the webServiceMessageReceiverHandlerAdapter
*/
@Bean
public WebServiceMessageReceiverHandlerAdapter webServiceMessageReceiverHandlerAdapter() {
WebServiceMessageReceiverHandlerAdapter adapter = new WebServiceMessageReceiverHandlerAdapter();
adapter.setMessageFactory(messageFactory());
return adapter;
}
/**
* Defines the {@link SimpleUrlHandlerMapping} as a Spring managed bean.
*
* Note: In order for the DispatcherServlet to handle SOAP message, we were forced to add the {@link WebServiceMessageReceiverHandlerAdapter}
* to the context. By explicitely adding that bean to the context, we are now forced to also add this bean to handle REST messages.
*
* @return the simpleUrlHandlerMapping
*/
@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
simpleUrlHandlerMapping.setDefaultHandler(messageDispatcher());
Properties urlProperties = new Properties();
urlProperties.put("Lookup.wsdl", "Lookup");
simpleUrlHandlerMapping.setMappings(urlProperties);
simpleUrlHandlerMapping.setDefaultHandler(messageDispatcher());
return simpleUrlHandlerMapping;
}
/**
* Defines the {@link SoapMessageDispatcher} as a Spring managed bean.
*
* Note: Dispatches SOAP messages.
*
* @return the messageDispatcher
*/
@Bean
public SoapMessageDispatcher messageDispatcher() {
return new SoapMessageDispatcher();
}
/**
* Defines the {@link SimpleControllerHandlerAdapter} as a Spring managed bean.
*
* Note: In order for the DispatcherServlet to handle SOAP messages, we were forced to add the {@link WebServiceMessageReceiverHandlerAdapter}
* to the context. By explicitely adding that bean to the context, the default adapters were not automatically added to handle
* standard MVC Controllers. We must add this bean to do that.
*
* @return the simpleControllerHandlerAdapter
*/
@Bean
public SimpleControllerHandlerAdapter simpleControllerHandlerAdapter() {
return new SimpleControllerHandlerAdapter();
}
Web 服务配置
删除了@ComponentScan("com.mycompany") - WebApplicationConfiguration 已经为我完成了这个,实际上在这个配置中获取了所有的 beans .
添加了以下bean
@Bean
public WsdlDefinitionHandlerAdapter wsdlDefinitionHandlerAdapter() {
WsdlDefinitionHandlerAdapter wsdlDefinitionHandlerAdapter = new WsdlDefinitionHandlerAdapter();
wsdlDefinitionHandlerAdapter.setTransformLocations(true);
return wsdlDefinitionHandlerAdapter;
}
注意!!! - 在 WsdlDefinitionHandlerAdapter 的 bean 定义中,我将 setter 称为 transformLocations。这与 WebApplicationConfig 中的 urlMapping 一起是公开 WSDL 的关键。当然其他 bean 是必需的,但这是原始问题的要点。
我正在尝试使用 Java-Config 设置 Web 服务项目(目前是 SOAP - 但最终会添加 REST)。不幸的是,我无法自动公开 WSDL(据推测,Spring 可以根据 XSD 生成它并公开它)。
我找到的唯一文档在定义 servlet (web.xml) 时使用了 xml 配置。
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
如何使用 Java-config 完成此操作?
WebApplicationInitializer
public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* {@inheritDoc}
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {WebSecurityConfiguration.class};
}
/**
* {@inheritDoc}
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {WebServicesConfiguration.class};
}
/**
* {@inheritDoc}
*/
@Override
protected String[] getServletMappings() {
// get all mappings
return new String[] { "/" };
}
Web 服务配置
@EnableWs
@Configuration
@ComponentScan("com.questsoftware")
public class WebServicesConfiguration extends WsConfigurationSupport {
@Bean
public XsdSchema schema() {
return new SimpleXsdSchema(new ClassPathResource("lookup.xsd"));
}
@Bean
public DefaultWsdl11Definition defaultWsdl11Definition() {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setTargetNamespace("http://com/[mycompany]/Lookup");
wsdl11Definition.setPortTypeName("Lookup");
wsdl11Definition.setLocationUri("/Lookup");
wsdl11Definition.setSchema(schema());
return wsdl11Definition;
}
需要说明的是 - 我还有一个 WebApplicationConfiguration (REST) - 但我还没有将它添加到 WebApplicationInitializer 中(运行 先解决这个问题)。
WebApplicationConfiguration
@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Configuration
@ComponentScan("com.mycompany")
public class WebApplicationConfiguration extends WebMvcConfigurerAdapter {
/**
* Defines {@link JdbcTemplate} as a Spring managed bean.
*
* @return
*/
@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
public DataSource dataSource() {
...
return dataSource;
}
/**
* Defines a {@link ViewResolver} as a Spring managed bean.
*
* @return the viewResolver
*/
@Bean
public ViewResolver viewResolver() {
final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages");
resolver.setSuffix(".jsp");
return resolver;
}
/**
* Registers resource handlers with Spring.
*
* @param registry the {@link ResourceHandlerRegistry}
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/view/**").addResourceLocations("/view/");
}
参考
http://docs.spring.io/spring-ws/sites/2.0/reference/html/server.html#server-automatic-wsdl-exposure
我想出了一个解决方案,但我不完全确定它是 'correct' 还是 'preferred' 方式。
我遵循了教程并简单地在基于 java-config 的 WebApplicationConfiguration 中定义了 beans,这些 bean 在基于教程 xml 的配置中定义。 --我的理解是,真的没有根本的区别,你只需要将 bean 添加到 Spring 容器中,所以 Spring 可以发挥它的魔力。
我对上面的代码进行了以下更改:
WebApplicationConfiguration
添加了以下bean
/**
* Defines the {@link SaajSoapMessageFactory} as a Spring managed bean.
*
* Note: we need a Soap implementation of a MessageFactory to create soap messages in Spring-WS.
*
* @return the messageFactory
*/
@Bean
public SaajSoapMessageFactory messageFactory() {
return new SaajSoapMessageFactory();
}
/**
* Defines the {@link WebServiceMessageReceiverHandlerAdapter} as a Spring managed bean.
*
* Note: We need to add this bean to the context in order for the DispatcherServlet to delegate to a MessageDispatcher
* (as opposed to Controllers). The MessageDispatcher is necessary for Spring-WS.
*
* @return the webServiceMessageReceiverHandlerAdapter
*/
@Bean
public WebServiceMessageReceiverHandlerAdapter webServiceMessageReceiverHandlerAdapter() {
WebServiceMessageReceiverHandlerAdapter adapter = new WebServiceMessageReceiverHandlerAdapter();
adapter.setMessageFactory(messageFactory());
return adapter;
}
/**
* Defines the {@link SimpleUrlHandlerMapping} as a Spring managed bean.
*
* Note: In order for the DispatcherServlet to handle SOAP message, we were forced to add the {@link WebServiceMessageReceiverHandlerAdapter}
* to the context. By explicitely adding that bean to the context, we are now forced to also add this bean to handle REST messages.
*
* @return the simpleUrlHandlerMapping
*/
@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
simpleUrlHandlerMapping.setDefaultHandler(messageDispatcher());
Properties urlProperties = new Properties();
urlProperties.put("Lookup.wsdl", "Lookup");
simpleUrlHandlerMapping.setMappings(urlProperties);
simpleUrlHandlerMapping.setDefaultHandler(messageDispatcher());
return simpleUrlHandlerMapping;
}
/**
* Defines the {@link SoapMessageDispatcher} as a Spring managed bean.
*
* Note: Dispatches SOAP messages.
*
* @return the messageDispatcher
*/
@Bean
public SoapMessageDispatcher messageDispatcher() {
return new SoapMessageDispatcher();
}
/**
* Defines the {@link SimpleControllerHandlerAdapter} as a Spring managed bean.
*
* Note: In order for the DispatcherServlet to handle SOAP messages, we were forced to add the {@link WebServiceMessageReceiverHandlerAdapter}
* to the context. By explicitely adding that bean to the context, the default adapters were not automatically added to handle
* standard MVC Controllers. We must add this bean to do that.
*
* @return the simpleControllerHandlerAdapter
*/
@Bean
public SimpleControllerHandlerAdapter simpleControllerHandlerAdapter() {
return new SimpleControllerHandlerAdapter();
}
Web 服务配置
删除了@ComponentScan("com.mycompany") - WebApplicationConfiguration 已经为我完成了这个,实际上在这个配置中获取了所有的 beans .
添加了以下bean
@Bean
public WsdlDefinitionHandlerAdapter wsdlDefinitionHandlerAdapter() {
WsdlDefinitionHandlerAdapter wsdlDefinitionHandlerAdapter = new WsdlDefinitionHandlerAdapter();
wsdlDefinitionHandlerAdapter.setTransformLocations(true);
return wsdlDefinitionHandlerAdapter;
}
注意!!! - 在 WsdlDefinitionHandlerAdapter 的 bean 定义中,我将 setter 称为 transformLocations。这与 WebApplicationConfig 中的 urlMapping 一起是公开 WSDL 的关键。当然其他 bean 是必需的,但这是原始问题的要点。