首页 > 代码库 > cxf的soap风格+spirng4+maven 服务端

cxf的soap风格+spirng4+maven 服务端

简介

SOAP

比较复杂,基于XML,有对应规范;REST利用HTTP请请求方式GET,POST,PUT,delete约定具体操作。简单的说,SOAP通过传输XML,XML定义了请求和响应的具体数据,要进行的操作等等.

REST

另一种约定,比如请求/user/100这个RUL,GET方式返回id为100的user信息,put方式则是更新id为1001的user信息,DELETE删除等。

接下来记录搭建cxf服务端

1、pom.xml文件

<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.jacky</groupId>
    <artifactId>cxf_demo</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>cxf_demo Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-common</artifactId>
            <version>2.5.4</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-core</artifactId>
            <version>2.6.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>2.6.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
        <finalName>cxf_demo</finalName>
    </build>
</project>

2、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://cxf.apache.org/jaxws   
    http://cxf.apache.org/schemas/jaxws.xsd">  
    <import resource="classpath:META-INF/cxf/cxf.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-extension-xml.xml"/>   
    <bean id="getInfoServiceImpl" class="com.jacky.impl.GetInfoServiceImpl"></bean>  
    <!-- wsdl地址为 http://www.localhost:8080/cxf_demo/cxf/getInfoService?wsdl -->
    <jaxws:endpoint id="getInfoService" implementor="#getInfoServiceImpl" address="/getInfoService"></jaxws:endpoint>  
</beans> 

3、Info.java

package com.jacky.entity;

public class Info {
    private String name;  
    private int age;  
    public String getName()  
    {  
        return name;  
    }  
    public void setName(String name)  
    {  
        this.name = name;  
    }  
    public int getAge()  
    {  
        return age;  
    }  
    public void setAge(int age)  
    {  
        this.age = age;  
    }  
}

4、GetInfoService.java

package com.jacky;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import com.jacky.entity.Info;

@WebService()
public interface GetInfoService {
    @WebMethod(operationName = "add")  
    @WebResult(name = "result")  
    public int add(@WebParam(name = "number1") int number1, @WebParam(name = "number2") int number2); 
    
    @WebMethod(operationName = "getInfo")  
    @WebResult(name = "result")  
    public Info getInfo(@WebParam(name = "name") String name, @WebParam(name = "age") int age);
}

5、GetInfoServiceImpl.java

package com.jacky.impl;

import javax.jws.WebService;

import com.jacky.GetInfoService;
import com.jacky.entity.Info;
/*
 * targetNamespace 的值是服务实现类的接口包的倒序,例如我的接口全路径为com.jacky.GetInfoService,则
 * 名称空间为http://jacky.com/,否则,客户端动态调用会找不到方法
 * */
@WebService(endpointInterface = "com.jacky.GetInfoService",targetNamespace="http://jacky.com/") 
public class GetInfoServiceImpl implements GetInfoService {

    @Override
    public int add(int number1, int number2) {
        return number1+number2;
    }

    @Override
    public Info getInfo(String name, int age) {
        Info info = new Info();
        info.setName(name);
        info.setAge(age);
        return info;
    }

}

6.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name> maven project</display-name>
   <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <servlet>  
        <servlet-name>CXFService</servlet-name>  
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
   </servlet>  
   <servlet-mapping>  
        <servlet-name>CXFService</servlet-name>  
        <url-pattern>/cxf/*</url-pattern>  
   </servlet-mapping>  
</web-app>

到了这里就soap服务端就搭建好了,可以测试了

地址:http://www.localhost:8080/cxf_demo/cxf/getInfoService?wsdl

 

cxf的soap风格+spirng4+maven 服务端