CXF 在Tomact 下发布一个服务

来源:互联网 发布:前端之巅 知乎 编辑:程序博客网 时间:2024/06/05 17:02

这个是看博客,参考资料,同时,自己也想试试到底效果怎么样,毕竟实际使用一下子才知道整个东西,到底的效果怎么样。

  1. 创建一个web工程,ideal 抽筋了?笔者试了好几次还是不行,最后命令行搞定 maven 3
    整个没必要记住,不会,百度一下,Google…
mvn archetype:generate -DgroupId=com.study -DartifactId=TomactCxf -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
  1. 配置maven
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.study</groupId>    <artifactId>TomactCxf</artifactId>    <packaging>war</packaging>    <version>1.0-SNAPSHOT</version>    <name>TomactCxf</name>    <url>http://maven.apache.org</url>    <properties>        <!-- CXF版本 -->        <cxf.version>3.1.1</cxf.version>    </properties>    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>3.8.1</version>            <scope>test</scope>        </dependency>        <!-- CXF -->        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-frontend-jaxws</artifactId>            <version>${cxf.version}</version>        </dependency>        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-transports-http</artifactId>            <version>${cxf.version}</version>        </dependency>        <!-- End CXF -->        <!-- 由于CXFServlet需要Spring Web的支持 -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>4.1.7.RELEASE</version>        </dependency>    </dependencies>    <build>        <finalName>TomactCxf</finalName>    </build></project>
  1. 在Web.xml中去配置servlet这个是启动服务的一个类,如果你去看看源码,可以知道,他怎么知道你的配置文件呢?
    具体就不去纠结了,只要会使用就好了,下面就是读取整个配置信息,然后在整个进程中发布服务,服务怎么知道,就是这个配置处理的事情了
 servletConfig.getServletContext().getResourceAsStream("/WEB-INF/cxf-servlet.xml");

web.xml

<!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>  <display-name>Archetype Created Web Application</display-name>    <!-- CXF Servlet -->    <servlet>        <servlet-name>cxfservlet</servlet-name>        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>cxfservlet</servlet-name>        <!-- 匹配/services下的所有请求 -->        <url-pattern>/services/*</url-pattern>    </servlet-mapping>    <!-- End CXF Servlet -->    <welcome-file-list>        <welcome-file>index.html</welcome-file>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>
  1. cxf-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:jaxws="http://cxf.apache.org/jaxws"       xmlns:soap="http://cxf.apache.org/bindings/soap"       xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">    <jaxws:server id="helloWs" serviceClass="com.hello.hello" address="/hello">      <!--  接口的地址-->        <jaxws:serviceBean>          <!--  实现类的地址-->            <bean class="com.hello.impl.helloImpl" />        </jaxws:serviceBean>    </jaxws:server></beans>

整个注解是怎么回事,可以参考上一篇博客 ,我只书写了最基本的定义

package com.hello;import javax.jws.WebMethod;import javax.jws.WebService;@WebServicepublic interface hello {    @WebMethod    public String getName(String name);}package com.hello.impl;import com.hello.hello;import javax.jws.WebService;@WebService(endpointInterface = "com.hello.impl.helloImpl")public class helloImpl implements hello {    public String getName(String name) {        return null;    }}

访问地址 :http://localhost:8888/CXF/services/hello?wsdl

如下很多默认的信息~~哈哈 慢慢的观看一下子!

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://hello.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="helloService" targetNamespace="http://hello.com/"><wsdl:types><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://hello.com/" elementFormDefault="unqualified" targetNamespace="http://hello.com/" version="1.0"><xs:element name="getName" type="tns:getName"/><xs:element name="getNameResponse" type="tns:getNameResponse"/><xs:complexType name="getName"><xs:sequence><xs:element minOccurs="0" name="arg0" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="getNameResponse"><xs:sequence><xs:element minOccurs="0" name="return" type="xs:string"/></xs:sequence></xs:complexType></xs:schema></wsdl:types><wsdl:message name="getName"><wsdl:part element="tns:getName" name="parameters"></wsdl:part></wsdl:message><wsdl:message name="getNameResponse"><wsdl:part element="tns:getNameResponse" name="parameters"></wsdl:part></wsdl:message><wsdl:portType name="hello"><wsdl:operation name="getName"><wsdl:input message="tns:getName" name="getName"></wsdl:input><wsdl:output message="tns:getNameResponse" name="getNameResponse"></wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="helloServiceSoapBinding" type="tns:hello"><soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operation name="getName"><soap:operation soapAction="" style="document"/><wsdl:input name="getName"><soap:body use="literal"/></wsdl:input><wsdl:output name="getNameResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="helloService"><wsdl:port binding="tns:helloServiceSoapBinding" name="helloPort"><soap:address location="http://localhost:8888/CXF/services/hello"/></wsdl:port></wsdl:service>

github: https://github.com/WangJi92/CXFDemo/tree/CXFTomcat

0 0