2016-09-20 5 views
1

Java-Configを使用して、webservicesプロジェクトをセットアップしようとしています(現在はSOAPですが、最終的にRESTを追加します)。残念ながら、私はWSDLを自動的に公開することができませんでした(おそらく、SpringはXSDに基づいてそれを生成し、公開することができます)。Spring-ws javaconfigロケーション変換

サーブレット(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[] { "/" }; 
} 

WebServicesConfiguration

@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でそれを追加していない(RAN最初にこの問題全体に渡って)。

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

答えて

0

私は解決策を考え出すことができたが、私はそれが '正しい' かだ場合、完全にはわかりません'好ましい'方法。

チュートリアルに続いて、チュートリアルのxmlベースの設定で定義されたWebアプリケーションのコンフィグレーションに基づいて、JavaのコンフィグレーションベースのBeanを定義しました。私の理解は、基本的な違いはなく、SpringコンテナにBeanを追加するだけで済むので、Springは魔法を働かせることができます。

私は以下のように変更して上記のコード更新しました:

WebApplicationConfiguration

を追加し、以下の豆

/** 
* 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(); 
} 

WebServicesConfiguration

が削除されました@ComponentScan( "com.mycompany") - WebApplicationConfigurationはすでにこれを実行していましたが、実際にはこの設定ですべてのBeanを取得します。

は、次のBean

@Bean 
public WsdlDefinitionHandlerAdapter wsdlDefinitionHandlerAdapter() { 

    WsdlDefinitionHandlerAdapter wsdlDefinitionHandlerAdapter = new WsdlDefinitionHandlerAdapter(); 
    wsdlDefinitionHandlerAdapter.setTransformLocations(true); 

    return wsdlDefinitionHandlerAdapter; 
} 

注追加しました! - WsdlDefinitionHandlerAdapterのBean定義で、transformLocationsのsetterを呼び出します。これは、WebApplicationConfigのurlMappingと一緒に、WSDLを公開するための鍵です。もちろん他の豆が必要ですが、これは元の質問の要点でした。

関連する問題