2014-01-19 11 views
15

Tomcat 7 MavenプラグインとCXFを使用してJAX-WSエンドポイントをデプロイしようとしています 2.7.8。私は好みの問題として、 のSpringまたはCXFのXML設定を望んでいません。 cxf-servlet.xml とCXFServletを使っていくつかのブログ、記事、投稿を見ることができますが、Javaの設定を完全に使用するものはありません。 CXFServletのソースコードを調べると、cxf-servlet.xmlなどのサーブレットコンテキスト内のキー'config-location'が検索されます。 cxf-servlet.xmlの代わりにプログラムでエンドポイントを登録しようとしましたが、機能しません。私はサービスにアクセスするときに404を得る。何か案は?Apache CXF + Spring Java設定(XMLなし)

@Configuration 
@ImportResource({ "classpath:META-INF/cxf/cxf.xml" }) 
public class CXFConfig { 
    @Autowired 
    Bus cxfBus; 

    // More code 

    @Bean 
    public Endpoint calculator() { 
     EndpointImpl endpoint = new EndpointImpl(cxfBus, new Calculator()); 
     endpoint.setAddress("/CalculatorService"); 
     return endpoint; 
    } 
} 

答えて

8

上記のendpoint.publish()呼び出しがすべて必要です。

+0

Javaベースの設定で複数のエンドポイントを追加するにはどうすればよいですか? –

+0

@HarmeetSingh私はそれを試していませんでしたが、エンドポイントを返す別のメソッドを作成しようとしましたか? –

+1

はい、私は試して、それは正常に動作します。 –

1

私はあなたがfactory.setServiceBeans内のあなたの豆を渡した場合、それはCXFは、純粋な春のJava設定で実行するようになってまでこのスレッドは間違いなく右のトラックに私を置く

package br.com.app.spring; 

import java.util.Arrays; 

import javax.ws.rs.ext.RuntimeDelegate; 

import org.apache.cxf.bus.spring.SpringBus; 
import org.apache.cxf.endpoint.Server; 
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; 
import org.codehaus.jackson.jaxrs.JacksonJsonProvider; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.ImportResource; 

import br.com.app.JaxRsApiApplication; 
import br.com.app.services.EditionRest; 
import br.com.app.services.EditionService; 

@Configuration 
@ImportResource(
    { 
     "classpath:META-INF/cxf/cxf.xml", 
     "classpath:META-INF/cxf/cxf-extension-xml.xml", 
     "classpath:META-INF/cxf/cxf-servlet.xml" 
    }) 
public class CXFConfig { 

@Bean(destroyMethod = "shutdown") 
public SpringBus cxf() { 
    return new SpringBus(); 
} 

@Bean 
public EditionService editionRest() { 
    return new EditionRest(); 
} 

@Bean 
public JaxRsApiApplication jaxRsApiApplication() { 
    return new JaxRsApiApplication(); 
} 

@Autowired 
@Bean 
public Server jaxRsServer(JacksonJsonProvider provider) { 

    JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance().createEndpoint(jaxRsApiApplication(), JAXRSServerFactoryBean.class); 
    factory.setServiceBeans(Arrays.<Object> asList(editionRest())); 
    factory.setProviders(Arrays.<Object> asList(provider)); 

    return factory.create(); 
} 

@Bean 
public JacksonJsonProvider jsonProvider() { 
    return new JacksonJsonProvider(); 
} 
} 
+1

彼はno xmlを要求しました... – Cuga

4

を動作することを信じて、しかしそれはdidnの必要なものすべてを提供する。

自分の純粋なJava構成では、web.xmlファイルがないことを意味していますが、私はこの答えが存在すると考えています。例えばSpring Bootはweb.xmlファイルを使用しません。

したがって、XMLファイルをまったく使用せずにCXFエンドポイントを登録するには、CXFServletもロードする構成ファイルが必要です。

import org.apache.cxf.Bus; 
import org.apache.cxf.jaxws.EndpointImpl; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.ImportResource; 

import javax.xml.ws.Endpoint; 

@Configuration 
@ImportResource({"classpath:META-INF/cxf/cxf.xml"}) 
public class JavaConfiguration { 

    @Autowired 
    private Bus bus; 

    @Bean 
    public Endpoint myServiceEndpoint() { 
     EndpointImpl endpoint = new EndpointImpl(bus, new MyService()); 
     endpoint.publish("/myservice"); 
     return endpoint; 
    } 

    @Bean 
    public ServletRegistrationBean cxfServlet() { 
     ServletRegistrationBean servlet = new ServletRegistrationBean(new CXFServlet(), "/services/*"); 
     servlet.setLoadOnStartup(1); 
     return servlet; 
    } 
} 

上記は、Spring内でCXFエンドポイントを正常にロードするために必要なすべての設定です。

これを示すsmall projectも作成しました。

+0

あなたは 'CXFServlet'の登録について正しいです。私はそれを登録しましたが、CXF設定と同じ場所にいなかったので、私の答えにそれを示していませんでした。 web.xmlのSpring Javaの代替手段はCXFとは関係がなく、より多くの設定が必要な場合は、上記のようにすることはできません。 web.xmlのSpringの代替は 'AbstractAnnotationConfigDispatcherServletInitializer'です - [ここにある](https://github.com/abhijitsarkar/jax-ws/blob/master/jax-ws-instrumentation/src/main/java/name/abhijitsarkar/ webservices/jaxws/instrumentation/config/WebAppInitializer.java)を実行する方法について説明します。 –

+1

しかし、あなたのJavaにxmlをインポートしています...どうして "純粋なJava"ですか? – david

7

ここに掲載されたすべてのものは、100%XML設定が無料ではありません。すべての投稿は、ウェブのほとんどのチュートリアルでも使用されているclasspath:META-INF/cxf/cxf.xmlを使用しています。しかしそれには解決策があります:org.apache.cxf.bus.spring.SpringBusを@Beanとして定義し、org.apache.cxf.Busから送られてくるname = Bus.DEFAULT_BUS_IDを設定します。

その他の回答で説明したように、Beans SpringBusとSEI実装クラスの転送を含め、org.apache.cxf.jaxws.EndpointImplをインスタンス化する必要があります。また、)(公開 - EndpointImplの方法は終わるURLを含む文字列を含む、becalledしている:あなたはSpringBootと一緒にApache CXFについての詳細を知りたい場合は

package de.jonashackt.tutorial.configuration; 

import javax.xml.ws.Endpoint; 

import org.apache.cxf.Bus; 
import org.apache.cxf.bus.spring.SpringBus; 
import org.apache.cxf.jaxws.EndpointImpl; 
import org.apache.cxf.transport.servlet.CXFServlet; 
import org.springframework.boot.context.embedded.ServletRegistrationBean; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 

import de.codecentric.namespace.weatherservice.WeatherService; 
import de.jonashackt.tutorial.endpoint.WeatherServiceEndpoint; 

@Configuration 
public class WebServiceConfiguration { 

    @Bean 
    public ServletRegistrationBean dispatcherServlet() { 
     return new ServletRegistrationBean(new CXFServlet(), "/soap-api/*"); 
    } 

    @Bean(name = Bus.DEFAULT_BUS_ID) 
    public SpringBus springBus() { 
     return new SpringBus(); 
    }  

    @Bean 
    public WeatherService weatherService() { 
     return new WeatherServiceEndpoint(); 
    } 

    @Bean 
    public Endpoint endpoint() { 
     EndpointImpl endpoint = new EndpointImpl(springBus(), weatherService()); 
     endpoint.publish("/WeatherSoapService"); 
     return endpoint; 
    } 
} 

、私はthis github projectの表情をお勧めします。

+0

"XMLフリー"という意味にかかっています。元の記事は、XMLを書かずにJavaクライアントを設定することでした。誰もJavaの設定からXMLをインポートしないことについては何も言わなかった。 'cxf.xml'をインポートすると、クライアントはコードを書く必要がなくなります。私はいつも自分のコードでそれを好むでしょう。 –

+1

私にとって、「XMLなし」とは、Spring 4.x(Spring Bootを含む)からJava Configを使用してBeanを定義することを意味します。 cxf.xmlは、BeanをSpringのXML構成で定義します。ところで、cxf.xmlをインポートしてAutowired Beanを宣言するには3行が必要です - "XMLなし"のアプローチと同じです:) – jonashackt

+0

'@ImportResource({" classpath:META-INF/cxf/cxf.xml " }) '3行のコード? –

0

あなたは春のブートを使用している場合は、使用することができます。

<dependency> 
    <groupId>org.apache.cxf</groupId> 
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId> 
    <version>${cxf.version}</version> <!-- 3.1.7 or higher --> 
</dependency> 

をエンドポイントに追加するには:

import javax.xml.ws.Endpoint; 
import org.apache.cxf.Bus; 
import org.apache.cxf.jaxws.EndpointImpl; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 

@Configuration 
public class WebServiceConfig { 

    @Bean 
    public Endpoint endpoint(Bus cxfBus) { 
     EndpointImpl endpoint = new EndpointImpl(cxfBus, new Calculator()); 
     endpoint.publish("/CalculatorService"); 
     return endpoint; 
    } 
} 

Official documentation of the CXF-Boot integrationを。

+0

これは4年前の質問です。すでにSpringブートを使用している人は誰もCXFを使用していません。 –

+0

@Abhijit私は同意しません。質問が尋ねられたときに解決策が存在しなかった場合、古い質問に新しい回答を追加することには何も問題ありません。また、 'cxf-spring-boot-starter-jaxws'の存在は、人々がBootでCXFを使用することを示しています。 – imgx64

関連する問題