• Spring下使用开发webservice


    依赖包

    <!-- CXF Dependencies -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.0.1</version>
    </dependency>
    

    web.xml配置

    web.xml中首先在contextConfigLocation中配置applicationContext-server.xml的路径,并配置CXFService,路径为/service/*,指定webservice的访问路径为localhost:8080/service/*

    <context-param>
    	<param-name>contextConfigLocation</param-name>
    	<param-value>classpath:com/nanri/config/springConfig/applicationContext.xml,
    			classpath:com/nanri/config/webserviceConfig/applicationContext-server.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>/service/*</url-pattern>
    </servlet-mapping>

    applicationContext-server.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-4.0.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context-4.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-extension-soap.xml" />
    	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
     -->
    	<bean id="userServiceBean" class="com.nanri.webservice.serviceTest2.ServiceImpl" />
    
    	<!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
    	<jaxws:server id="userService" serviceClass="com.nanri.webservice.serviceTest2.IService"
    		address="/Users">
    		<jaxws:serviceBean>
    			<!-- 要暴露的 bean 的引用 -->
    			<ref bean="userServiceBean" />
    		</jaxws:serviceBean>
    	</jaxws:server>
    </beans>
    

    服务器端Java类

    包结构如下:

    对象User

    package com.nanri.webservice.serviceTest2;
    
    public class User {  
        int id ;  
        String name = null;  
        String address = null;    
        String email = null;  
        public int getId() {  
            return id;  
        }  
        public void setId(int id) {  
            this.id = id;  
        }  
        public String getName() {  
            return name;  
        }  
        public void setName(String name) {  
            this.name = name;  
        }     
        public String getAddress() {  
            return address;  
        }  
        public void setAddress(String address) {  
            this.address = address;  
        }     
        public String getEmail() {  
            return email;  
        }  
        public void setEmail(String email) {  
            this.email = email;  
        }         
    }  
    

     服务接口IService

    package com.nanri.webservice.serviceTest2;
    
    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.Style; 
    @WebService  
    @SOAPBinding(style = Style.RPC)  
    public interface IService {  
        public User getUserByName(@WebParam(name = "name") String name);  
        public void setUser(User user);  
    }  
    

     服务接口实现ServiceImpl

    package com.nanri.webservice.serviceTest2;
    
    import java.util.Date;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.Style;
    
    import org.springframework.stereotype.Repository;
    
    
    /**
     * <b>function:</b> WebService传递复杂对象,如JavaBean、Array、List、Map等
     */
    @WebService
    @SOAPBinding(style = Style.RPC)
    @SuppressWarnings("deprecation")
    public class ServiceImpl implements IService {
    	public User getUserByName(@WebParam(name = "name") String name) {
    		User user = new User();
    		user.setId(new Date().getSeconds());
    		user.setName(name);
    		user.setAddress("china");
    		user.setEmail(name + "@test.com");
    		return user;
    	}
    
    	public void setUser(User user) {
    		System.out.println("############Server setUser###########");
    		System.out.println("setUser:" + user);
    	}
    }

    客户端

    SpringUsersWsClient

    package com.nanri.webservice.serviceTest2;
    
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
    
    public class SpringUsersWsClient {
    	public static void main(String[] args) {
    
    		// 调用WebService
    
    		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    		factory.setServiceClass(IService.class);
    		factory.setAddress("http://localhost:8080/SpringProject/service/Users");
    		IService service = (IService) factory.create();
    		System.out.println("#############Client getUserByName##############");
    		User user = service.getUserByName("hoojo");
    		System.out.println(user);
    		user.setAddress("China-Guangzhou");
    		service.setUser(user);
    	}
    }
    

     访问 http://localhost:8080/SpringProject/service/Users?wsdl ,看到wsdl文件即说明配置成功。

  • 相关阅读:
    转载的,讲解java.util的集合类
    LinkedList
    数据结构
    随笔
    spring boot初步尝试
    spring boot
    java8 新特性
    volatile 续
    java 线程基础学习
    java 基础
  • 原文地址:https://www.cnblogs.com/wuchaodzxx/p/7142537.html
Copyright © 2020-2023  润新知