spring 整合webservice

pom.xml文件

  1. <dependencies>
  2. <!-- CXF WS开发 -->
  3. <dependency>
  4. <groupId>org.apache.cxf</groupId>
  5. <artifactId>cxf-rt-frontend-jaxws</artifactId>
  6. <version>3.0.1</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-context</artifactId>
  11. <version>4.1.7.RELEASE</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework</groupId>
  15. <artifactId>spring-web</artifactId>
  16. <version>4.1.7.RELEASE</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-test</artifactId>
  21. <version>4.1.7.RELEASE</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>junit</groupId>
  25. <artifactId>junit</artifactId>
  26. <version>4.12</version>
  27. </dependency>
  28.  
  29. </dependencies>
  30. <build>
  31. <plugins>
  32. <plugin>
  33. <groupId>org.codehaus.mojo</groupId>
  34. <artifactId>tomcat-maven-plugin</artifactId>
  35. <version>1.1</version>
  36. <configuration>
  37. <port>9998</port>
  38. </configuration>
  39. </plugin>
  40.  
  41. <plugin>
  42. <groupId>org.apache.maven.plugins</groupId>
  43. <artifactId>maven-compiler-plugin</artifactId>
  44. <version>2.3.2</version>
  45. <configuration>
  46. <source>1.7</source>
  47. <target>1.7</target>
  48. </configuration>
  49. </plugin>
  50. </plugins>
  51. </build>

  web.xml文件

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>classpath:applicationContext.xml</param-value>
  4. </context-param>
  5. <listener>
  6. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  7. </listener>
  8. <servlet>
  9. <servlet-name>CXFService</servlet-name>
  10. <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  11. <load-on-startup>1</load-on-startup>
  12. </servlet>
  13. <servlet-mapping>
  14. <servlet-name>CXFService</servlet-name>
  15. <url-pattern>/services/*</url-pattern>
  16. </servlet-mapping>

  applicationContext.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  7.  
  8. <!--
  9.  
  10. serviceClass 服务接口
  11. address 服务访问地址
  12. -->
  13. <jaxws:server id="userService" address="/userService"
  14. serviceClass="com.baidu.service.UserService">
  15. <jaxws:serviceBean>
  16. <bean class="com.baidu.service.imp.UserServiceImp" />
  17. </jaxws:serviceBean>
  18. </jaxws:server>
  19.  
  20. </beans>

  

包结构

  UserService接口

  1. package com.baidu.service;
  2.  
  3. import javax.jws.WebMethod;
  4. import javax.jws.WebService;
  5.  
  6. import com.baidu.domain.User;
  7.  
  8. @WebService
  9. public interface UserService {
  10. @WebMethod
  11. public User get(Integer id);
  12. @WebMethod
  13. public void eat();
  14. }

  UserServiceImp实现类

  1. package com.baidu.service.imp;
  2.  
  3. import javax.jws.WebMethod;
  4. import javax.jws.WebService;
  5.  
  6. import com.baidu.domain.User;
  7. import com.baidu.service.UserService;
  8. @WebService
  9. public class UserServiceImp implements UserService {
  10.  
  11. public User get(Integer id) {
  12. if(id==1){
  13. User u=new User();
  14. u.setId(2);
  15. u.setName("张三");
  16. return u;
  17. }
  18. return null;
  19. }
  20.  
  21. @Override
  22. public void eat() {
  23. System.out.println("123");
  24.  
  25. }
  26.  
  27. }

  新建一个maven web project项目

pom.xml文件不变

applicationContext.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  7.  
  8. <!--
  9. address 客户端访问服务路径
  10. serviceClass 配置接口
  11. serviceBean 配置实现类 -->
  12. <jaxws:client id="userServiceClient"
  13. serviceClass="com.baidu.service.UserService"
  14. address="http://localhost:9998/werservicespring/services/userService" >
  15. <!-- 来源消息拦截器
  16. <jaxws:inInterceptors>
  17. <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
  18. </jaxws:inInterceptors> -->
  19. <!-- 输出消息拦截器
  20. <jaxws:outInterceptors>
  21. <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
  22. </jaxws:outInterceptors>-->
  23. </jaxws:client>
  24.  
  25. </beans>  

在测试之前需要创建接口  包结构也需要一样

  1. package com.baidu.service;
  2.  
  3. import javax.jws.WebMethod;
  4. import javax.jws.WebService;
  5.  
  6. import com.baidu.domain.User;
  7.  
  8. @WebService
  9. public interface UserService {
  10. @WebMethod
  11. public User get(Integer id);
  12. @WebMethod
  13. public void eat();
  14. }

  

测试类

  1. package com.baidu.test;
  2.  
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.test.context.ContextConfiguration;
  7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  8.  
  9. import com.baidu.domain.User;
  10. import com.baidu.service.UserService;
  11.  
  12. @RunWith(SpringJUnit4ClassRunner.class)
  13. @ContextConfiguration(locations="classpath:applicationContext.xml")
  14. public class Test01 {
  15. @Autowired
  16. private UserService userService;
  17. @Test
  18. public void test01(){
  19. //userService.eat();
  20. User user = userService.get(1);
  21. System.out.println(user);
  22. }
  23. }

  

webService之helloword(web)的更多相关文章

  1. webservice之helloword(web)rs

    spring整合webservice 1.pom.xml文件 <dependencies> <!-- cxf 进行rs开发 必须导入 --> <dependency> ...

  2. Python开发WebService:REST,web.py,eurasia,Django

    Python开发WebService:REST,web.py,eurasia,Django 博客分类: Python PythonRESTWebWebServiceDjango  对于今天的WebSe ...

  3. webService之helloword(java)rs

    webservice之rs(helloworld) 1.pom.xml文件 <dependencies> <!-- 使用CXF RS开发 --> <dependency& ...

  4. webService之helloword(java)

    webservice 远程数据交互技术 1.导入jar包(如果是 maven项目导入项目坐标) 2.创建服务 3.测试服务 我们使用maven来做测试服务 pom.xml文件 <project ...

  5. 用.NET WebService Studio调试Web Service解决SOAPAction的问题

    话说是这样的,这两天开发一个短信发送功能,客户给了一个 Web Service 地址(没有文档),让我调用就可以发送了, 我在VS 2013添加了服务引用,一切正常,可是执行代理方法时,怎么都报错 R ...

  6. 翻译-使用Spring WebService生成SOAP Web Service

    原文链接:http://spring.io/guides/gs/producing-web-service/ 生成SOAP web service 该指南将带领你使用Spring创建一个基于SOAP的 ...

  7. 用C#通过反射实现动态调用WebService 告别Web引用

    我们都知道,调用WebService可以在工程中对WebService地址进行WEB引用,但是这确实很不方便.我想能够利用配置文件灵活调用WebService.如何实现呢? 用C#通过反射实现动态调用 ...

  8. Web.config中设置启用webservice远程调试访问

    在.NET 中已经默认将webservice的远程调试功能关闭,有的时候我们需要远程调试程序的时候,就需要打开此功能我们只需在webservice的项目的中添web.config的<system ...

  9. Web.config中设置启用webservice远程调试访问 参数看不到

    <system.web><compilation debug="true" /> <!--begin启用webservice远程访问--> &l ...

随机推荐

  1. 获取URL某个参数

    /* 获取URL某个参数(可以是中文) * 返回:字符串 */ function getUrlParam(key) { // 获取参数 var url = window.location.search ...

  2. 前端基础之JavaScript day51

    前端基础之JavaScript   JavaScript概述 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中) ...

  3. mysql-mysqldump命令导出多个数据库结构(实战)

    环境:windows server 2012 打开CMD命令行模式, >cd c:\Program Files\Mysql\Mysql 5.7.1\bin c:\Program Files\My ...

  4. memcached 一致性hash原理

    memcache 是一个分布式的缓存系统,但是本身没有提供集群功能,在大型应用的情况下容易成为瓶颈.但是客户端这个时候可以自由扩展,分两阶段实现.第一阶段:key 要先根据一定的算法映射到一台memc ...

  5. 论坛:一对一关联映射/单向关联/两个类间,可以有两个(多个)关联关系/content为大文本类型/

    >>单向:只写一端的映射属性,另一端不写(有一端用不着);双向:两端都写映射属性 >>一对一关联有两类:一类基于主键的(一般不使用),一类基于外键的(重点学习): 外键:是一个 ...

  6. robot framework 中should be true 与should contain 的区别

    should be true  是否等于:判断是否should contain  是否包含 a是否包含b

  7. vue回到顶部组件

    html <template> <a href="javascript:;" class="toTop" @click="backT ...

  8. Sophus libSophus.so

    在编译包含Sophus的源文件的时候,出现如下错误 ../lib/libmyslam.so: undefined reference to `Sophus::SO3::SO3(double, doub ...

  9. Oracle 12c的可插拔数据库PDB

    1. 默认安装之后会有一个可插拔数据库:pdborcl 2. 启动根容器: [oracle@eric ~]$ export ORACLE_SID=orcl [oracle@eric ~]$ sqlpl ...

  10. oracl之导入dmp文件

    导入步骤比较简单SQL Develep->Tools->Import tables->选择上该dmp文件即可. 导出步骤也比较简单SQL Develep->Tools-> ...