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.iteye.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&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>
接口发布成功 后
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());
}
}
接口测试 结果:
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&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>
测试 跑通程序
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!