user 实例 来自 :
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 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 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
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">
1.4 test-servlet
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">
destroy-method="close">
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf8
root
root
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
1.5web.xml
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">
back/jsp/main.jsp
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
/WEB-INF/application-cxf.xml /WEB-INF/test-servlet.xml
test
org.springframework.web.servlet.DispatcherServlet
1
test
*.do
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
2
CXFServlet
/WebService/*
接口发布成功 后
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 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 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
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">
3.4 web.xml
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">
/WEB-INF/config.properties
destroy-method="close">
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf8
root
root
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="" p:suffix=".jsp">
测试 跑通程序
捐助开发者
在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!