8

私はApache CXFとTomcatにかなり慣れていません。シンプルな Web サービスを構築して tomcat にデプロイしようとしています。以下は私の web.xml ですが、ブラウザを使用して「サービス」フォルダにアクセスしようとすると、「サービスが見つかりませんでした」と表示されます。Java Web サービス クライアントを作成しようとしましたが、サービスを見つけることもできません。これで何が間違っている可能性がありますか?

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>Sample web service provider</display-name>
    <listener>
        <!-- For Metro, use this listener-class instead: 
             com.sun.xml.ws.transport.http.servlet.WSServletContextListener -->
        <listener-class>
              org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <!-- Remove below context-param element if using Metro -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
              classpath:META-INF/cxf/cxf.xml
        </param-value>
    </context-param>
    <servlet>
        <servlet-name>WebServicePort</servlet-name>
        <!-- For Metro, use this servlet-class instead: 
             com.sun.xml.ws.transport.http.servlet.WSServlet  -->
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>WebServicePort</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
</web-app>
4

3 に答える 3

7

これは、アプリケーションで公開されているサービスがないことを意味します。あなたweb.xmlは正しいようですが、Spring の構成という 1 つのことを見逃していました。web.xmlたとえば、次のように、Spring 構成の場所を に追加します。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>

また、Web サービス インターフェイスを実装するクラスを作成し、それを SpringapplicationContext.xml構成ファイルで CXF エンドポイントとして公開する必要があります。例:

<bean id="candidateImpl" class="some.pckg.CandidateImpl"/>

<jaxws:endpoint id="candidateEndpoint"
                implementor="#candidateImpl"
                address="/Candidate"
        />

CandidateImplクラスには@WebServiceアノテーションが必要です。例:

@WebService(targetNamespace = "http://something.com/ws/candidate",
        portName = "CandidateService",
        serviceName = "Candidate",
        endpointInterface = "some.pckg.types.CandidateService",
        wsdlLocation = "WEB-INF/wsdl/CandidateService.wsdl")
public class CandidateImpl implements CandidateService {
     //Implementation of all methods from CandidateService.
}

すべてを正しく行った場合は、次の場所で利用可能なサービスが 1 つ表示されるはずです。

http(s)://whateverhost.com:<somePort>/SomeContextPath/services

そして、次のように WSDL ファイルを取得できるはずです。

http(s)://whateverhost.com:<somePort>/SomeContextPath/services/Candidate?wsdl

以下も参照してください。

于 2013-02-28T10:51:20.100 に答える
1

web.xml でサーブレットを構成する必要があります。以下に例を示します。

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
       PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
       "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

   <servlet>
       <servlet-name>CXFServlet</servlet-name>
       <display-name>CXF Servlet</display-name>
       <servlet-class>
           org.apache.cxf.transport.servlet.CXFServlet
       </servlet-class>
       <init-param>
           <param-name>config-location</param-name>
           <param-value>/WEB-INF/spring-ws-servlet.xml</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
       <servlet-name>CXFServlet</servlet-name>
       <url-pattern>/services/*</url-pattern>
   </servlet-mapping>

</web-app>

ここで、WEB-INF フォルダーの下に spring-ws-servlet.xml という名前のファイルを定義する必要があります。以下は、Web サービスの実際の構成を含む spring-ws-servlet.xml のコンテンツの例です。もちろん、これはあなたのロジックに依存します:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd">

   <context:component-scan base-package="com.sample.service"/>

   <!-- JAX-WS Service Endpoint -->
   <bean id="personImpl" class="com.sample.service.impl.PersonServiceImpl"/>

   <jaxws:endpoint id="personEndpoint"
                   implementor="#personImpl"
                   address="/person">
       <jaxws:properties>
           <entry key="schema-validation-enabled" value="true"/>
       </jaxws:properties>
   </jaxws:endpoint>
   <!-- JAX-WS Service Endpoint End-->
</beans>

これにより、 http://localhost:8080/services/person?wsdlで Web サービスにアクセスできます。

これは、この投稿から取得されます。IntelliJ IdeaとSpringでCxfサービスを作るチュートリアルです

https://aldavblog.wordpress.com/2015/01/22/creating-a-web-service-from-scratch-using-spring-maven-apache-cxf/

于 2015-01-22T18:46:32.537 に答える