设为首页 收藏本站
查看: 560|回复: 0

[经验分享] (webservice+cxf+mybatis+mysql+springmvc) webservice + cxf 能够跑起来的cxf ,来这里,,

[复制链接]

尚未签到

发表于 2016-10-23 11:05:46 | 显示全部楼层 |阅读模式
  
  webservice jar 下载:  http://download.csdn.net/download/knight_black_bob/9186507
  webservice server  下载:http://download.csdn.net/download/knight_black_bob/9186521
  webservice client    下载:http://download.csdn.net/download/knight_black_bob/9186517
  综合下载:http://download.csdn.net/download/knight_black_bob/9186535
  
  user 实例 来自 :
  springmvc+mybatis+mysql 自动生成代码 http://knight-black-bob.iyunv.com/blog/2208162
  
  
  0 准备工作 jar 下载

cxf-2.6.1.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-jaxws_2.2_spec-1.1.jar
geronimo-ws-metadata_2.0_spec-1.1.3.jar
neethi-3.0.2.jar
slf4j-api-1.6.2.jar
wsdl4j-1.6.2.jar
xmlschema-core-2.0.2.jar
jetty-continuation-7.5.4.v20111024.jar
jetty-http-7.5.4.v20111024.jar
jetty-io-7.5.4.v20111024.jar
jetty-server-7.5.4.v20111024.jar
jetty-util-7.5.4.v20111024.jar
  
  
  
  1.webservice server
  1.1 service 接口

package cn.com.baoy.service;
import java.util.List;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import org.springframework.stereotype.Service;
import cn.com.baoy.bean.User;
/**
* @author baoyou  E-mail:curiousby@163.com
* @version 创建时间:2015年10月13日 上午11:13:28
* des:
*/
@WebService
@SOAPBinding(style = Style.RPC)
public interface UserService {

public  String sayHello(String string);
public List<User> allUser();
public void delUser(Integer userId);
public User user(Integer userId);
public void updateUser(User user);
public void addUser(User user);
}

  
  1.2 serviceimpl 接口实现
  

package cn.com.baoy.service.impl;
import java.util.List;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.baoy.bean.User;
import cn.com.baoy.dao.UserDao;
import cn.com.baoy.service.UserService;
@Service
@WebService(endpointInterface="cn.com.baoy.service.UserService", serviceName="UserService")
@SOAPBinding(style = Style.RPC)
public class UserServiceImpl implements UserService {

@Autowired
UserDao dao;
public  String sayHello(String string){
return "test : "+string;
}
public List<User> allUser(){
return dao.allUser();
}
public void delUser(Integer userId){
dao.delUser(userId);
}
public User user(Integer userId){
return dao.user(userId);
}
public void updateUser(User user){
dao.updateUser(user);
}
public void addUser(User user){
dao.addUser(user);
}
}

  
  1.3application-cxf.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
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"
default-autowire="byName">
<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="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>   
<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>  
<jaxws:endpoint implementor="cn.com.baoy.service.impl.UserServiceImpl" address="/userService"></jaxws:endpoint>
</beans>  
  
  1.4 test-servlet
  

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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 "
default-autowire="byName">

<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/database?useUnicode=true&amp;characterEncoding=utf8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>

<!-- 自动扫描controller bean,把作了注解的类转换为bean -->
<context:component-scan base-package="cn.com.baoy" />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<!-- <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="" p:suffix=".jsp">
<property name="order" value="0" />
</bean> -->
<!-- 创建SqlSessionFactory,同时指定数据源 -->  
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:cn/com/baoy/mapper/*.xml" />
</bean>  
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.com.baoy.dao" />
</bean>

</beans>  
  
  1.5web.xml
  
  

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>back/jsp/main.jsp</welcome-file>
</welcome-file-list>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-cxf.xml /WEB-INF/test-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/WebService/*</url-pattern>
</servlet-mapping>
</web-app>
  
  
  接口发布成功 后
  
DSC0000.png
 
DSC0001.jpg
 
  
  
  
  
  
  2.0 测试发布接口程序

package cn.com.baoy;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import cn.com.baoy.service.UserService;
public class WSTest {
public static void main(String[] args) {  
//创建WebService客户端代理工厂  
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
//注册WebService接口  
factory.setServiceClass(UserService.class);  
//设置WebService地址  
factory.setAddress("http://localhost:8080/webserviceserver/WebService/userService");  
UserService userService = (UserService)factory.create();  
System.out.println("invoke webservice...");  
System.out.println("message context is:"+userService.sayHello("baoyou"));  
System.out.println("message context is:"+userService.user(7).getUserName());  
}  
}

  
  
  接口测试 结果:
  
  
DSC0002.png
 
  
  
  3 webservice client
  
  3.1  接口定义
  

package cn.com.baoy.service;
import java.util.List;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import org.springframework.stereotype.Service;
import cn.com.baoy.bean.User;
/**
* @author baoyou  E-mail:curiousby@163.com
* @version 创建时间:2015年10月13日 上午11:13:28
* des:
*/
@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC)
public interface UserService {
public  String sayHello(String string);
public List<User> allUser();
public void delUser(Integer userId);
public User user(Integer userId);
public void updateUser(User user);
public void addUser(User user);
}

  
  3.2
  

package cn.com.baoy.controller;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.com.baoy.bean.User;
import cn.com.baoy.service.UserService;
@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping(value = "/userView.do")
public String userView(ModelMap modelMap,String pageNo, String choice, HttpSession session){
List<User> userList = userService.allUser();
modelMap.put("userList", userList);
return "back/jsp/user/userView";
}
@RequestMapping(value = "/userDel{userId}.do")
public String userDel(@PathVariable("userId")int userId ,ModelMap modelMap,String pageNo, String choice, HttpSession session){
userService.delUser(userId);
String message1="删除成功";
String message2="请返回……";
String  url="userView.do";
modelMap.put("message1", message1);
modelMap.put("message2", message2);
modelMap.put("url", url);
return "infomationShow";
}
@RequestMapping(value ="/userGoAdd.do")
public String userGoAdd(ModelMap modelMap,String pageNo, String choice, HttpSession session){
return "back/jsp/user/userAdd";
}
@RequestMapping(value = "/userAdd.do",method=RequestMethod.POST)
public String userAdd(User form,ModelMap modelMap,String pageNo, String choice, HttpSession session){
userService.addUser(form);
String message1="添加成功";
String message2="请返回……";
String  url="userView.do";
modelMap.put("message1", message1);
modelMap.put("message2", message2);
modelMap.put("url", url);
return "infomationShow";
}
@RequestMapping(value = "/userGoUpdate{userId}.do")
public String userGoUpdate(@PathVariable("userId")int userId ,ModelMap modelMap,String pageNo, String choice, HttpSession session){
User user = userService.user(userId);
modelMap.put("user", user);
return "back/jsp/user/userUpdate";
}
@RequestMapping(value = "/userUpdate.do",method=RequestMethod.POST)
public String userUpdate(User form,ModelMap modelMap,String pageNo, String choice, HttpSession session){
userService.updateUser(form);
String message1="修改成功";
String message2="请返回……";
String  url="userView.do";
modelMap.put("message1", message1);
modelMap.put("message2", message2);
modelMap.put("url", url);
return "infomationShow";
}
}

  
  
  3.3 application-service.xml

<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-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:client id="userService" serviceClass="cn.com.baoy.service.UserService" address="http://localhost:8080/webserviceserver/WebService/userService"/>
</beans>  
  
  3.4 web.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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 "
default-autowire="byName">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/config.properties</value>
</property>
</bean>

<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/database?useUnicode=true&amp;characterEncoding=utf8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>

<!-- 自动扫描controller bean,把作了注解的类转换为bean -->
<context:component-scan base-package="cn.com.baoy" />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="" p:suffix=".jsp">
<property name="order" value="0" />
</bean>
<!-- 创建SqlSessionFactory,同时指定数据源 -->  
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:cn/com/baoy/mapper/*.xml" />
</bean>  
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.com.baoy.dao" />
</bean>

</beans>  
  
  
  
  测试 跑通程序
  
DSC0003.png
 
  
  
  
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-290205-1-1.html 上篇帖子: 7-8章Mysql技术内幕InnoDB存储引擎——事务&备份&性能调优 下篇帖子: 如何创建数据库连接池(一个基于libmysql的MySQL数据库连接池示例)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表