当前位置 : 主页 > 网页制作 > Nodejs >

webservice cxf+spring maven项目

来源:互联网 收集:自由互联 发布时间:2021-06-24
创建maven web项目webservice 更改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-
  1. 创建maven web项目webservice
  2. 更改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</groupId>
  <artifactId>webService</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>webService Maven Webapp</name>
  <url>http://maven.apache.org</url>

   <properties>  
        <cxf.version>2.2.3</cxf.version>  
        <spring.version>3.2.3.RELEASE</spring.version>  
        <slf4j.version>1.7.7</slf4j.version>  
    </properties>  

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

        <!-- CXF Dependencies -->  
        <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>  
        <!-- Jetty is needed if you're are not using the CXFServlet -->  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-transports-http-jetty</artifactId>  
            <version>${cxf.version}</version>  
        </dependency>  
        <!-- End of CXF Dependencies -->  

        <!-- Spring Dependencies ${spring.version} -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  

        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  

        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-log4j12</artifactId>  
            <version>${slf4j.version}</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>  
                    <encoding>UTF-8</encoding>  
                </configuration>  
            </plugin>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-surefire-plugin</artifactId>  
                <configuration>  
                    <!-- 跳过测试单元 -->  
                    <skip>true</skip>  
                </configuration>  
            </plugin>  

            <plugin>  
                <groupId>org.mortbay.jetty</groupId>  
                <artifactId>jetty-maven-plugin</artifactId>  
            </plugin>              
        </plugins>  
    </build>  
</project>

3。 配置web .xml 加入spring和cxf

<!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>

    <!-- 设置Spring容器加载配置文件路径 -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:applicationContext*.xml</param-value>  
    </context-param>  

    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  

    <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>/ws/*</url-pattern>  
    </servlet-mapping>  

</web-app>

4。 创建applicationContext.xml 配置文件

注意:要引入classpath:META-INF/cxf/cxf.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"/>   
    <import resource="classpath:META-INF/cxf/cxf-extension-http.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <bean id="getInfoServiceImpl" class="serviceImpl.GetInfoServiceImpl"></bean>  
    <jaxws:endpoint id="getInfoService" implementor="#getInfoServiceImpl" address="/getInfoService"></jaxws:endpoint> 

</beans>

5.创建 GetInfoService 接口

package service;

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

import model.RetInfo;

@WebService
public interface GetInfoService {

    @WebMethod(operationName = "add")  
    @WebResult(name = "result")  
    public int add(@WebParam(name = "num1") int num1, @WebParam(name = "num2") int num2);

    @WebMethod(operationName = "getRetInfo")  
    @WebResult(name = "result")  
    public RetInfo getRetInfo(@WebParam(name = "name") String name, @WebParam(name = "age") int age);  
}

6.创建GetInfoServiceImpl实现接口GetInfoService

package serviceImpl;

import javax.jws.WebService;

import model.RetInfo;
import service.GetInfoService;

@WebService(endpointInterface = "service.GetInfoService")
public class GetInfoServiceImpl implements GetInfoService {

    @Override
    public int add(int num1, int num2) {
        // TODO Auto-generated method stub
        return num1 + num2;
    }

    @Override
    public RetInfo getRetInfo(String name, int age) {
        // TODO Auto-generated method stub
        RetInfo retInfo = new RetInfo();
        retInfo.setAge(age);
        retInfo.setName(name);
        return retInfo;
    }

}

7.创建 client applicationContext-client.xml client使用spring管理webservice

注意:address为服务端发布webservice的url

<?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">  

    <context:property-placeholder location="classpath:url.properties"/>

    <jaxws:client id="userWsClient" serviceClass="service.GetInfoService" address="http://localhost:8080/webService/ws/getInfoService?wsdl" />  

</beans>

8.创建client端测试代码

package model;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.GetInfoService;

public class Test {

    private static final Logger logger = LoggerFactory.getLogger(Test.class);

    public static void main(String[] args) {

        //加载Spring配置文件 
        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");  
        //从Spring容器中获取对象 
        GetInfoService userService = context.getBean("userWsClient",GetInfoService.class); 
        int sum = userService.add(2, 3);
        System.out.println(sum);
        logger.info("aaaaaaaaaaaaa");

    }
}

9.项目结构图

网友评论