webservice的创建,调用及客户端,服务端分离(使用maven工程)

一.项目结构
服务端
webservice的创建,调用及客户端,服务端分离(使用maven工程)_第1张图片
中间工程
webservice的创建,调用及客户端,服务端分离(使用maven工程)_第2张图片
客户端
webservice的创建,调用及客户端,服务端分离(使用maven工程)_第3张图片
二.详细配置过程(jdk使用的为1.7)
(一).服务端配置
1.Maven pom文件依赖


	4.0.0
	ztc
	springMVCMaven
	war
	0.0.1-SNAPSHOT
	springMVCMaven Maven Webapp
	http://maven.apache.org
		
		UTF-8
		3.1.4.RELEASE
		2.2.9
	
	
		
			junit
			junit
			3.8.1
			test
		
				
			ztc
 			ws-api
  			0.0.1-SNAPSHOT
		
		
			com.alibaba
			dubbo
			
				
					org.springframework
					spring
				
			
			2.5.3
		
		
			com.101tec
			zkclient
			0.10
		
		
		
			org.springframework
			spring-context
			${spring.vesion}
		
		
		
			mysql
			mysql-connector-java
			5.1.38
		
		
			org.springframework
			spring-aop
			${spring.vesion}
		
		
			org.springframework
			spring-asm
			${spring.vesion}
		
		
			org.springframework
			spring-aspects
			${spring.vesion}
		
		
			org.springframework
			spring-beans
			${spring.vesion}
		
		
			org.springframework
			spring-context
			${spring.vesion}
		
		
			org.springframework
			spring-context-support
			${spring.vesion}
		
		
			org.springframework
			spring-core
			${spring.vesion}
		
		
			org.springframework
			spring-expression
			${spring.vesion}
		
		
			org.springframework
			spring-instrument
			${spring.vesion}
		
		
			org.springframework
			spring-instrument-tomcat
			${spring.vesion}
		
		
			org.aspectj
			aspectjweaver
			1.6.9
		
		
			commons-pool
			commons-pool
			1.5.3
		
		
			commons-collections
			commons-collections
			3.2
		
		
			org.springframework
			spring-jms
			${spring.vesion}
		
		
			org.springframework
			spring-oxm
			${spring.vesion}
		
		
			org.springframework
			spring-web
			${spring.vesion}
		
		
			org.springframework
			spring-webmvc
			${spring.vesion}
		
		
			org.springframework
			spring-webmvc-portlet
			${spring.vesion}
		
		
			org.springframework
			spring-struts
			${spring.vesion}
		
		
			commons-httpclient
			commons-httpclient
			3.1
		
		
		
			org.slf4j
			slf4j-api
			1.5.10
		
		
			org.slf4j
			slf4j-log4j12
			1.5.10
		
		
			org.apache.ibatis
			ibatis-sqlmap
			2.3.0
		
		
			org.apache.ibatis
			ibatis-core
			3.0
		
		
		
			org.springframework
			spring-orm
			${spring.vesion}
		
		
			org.springframework
			spring-jdbc
			${spring.vesion}
		
		
			org.apache.geronimo.specs
			geronimo-servlet_2.5_spec
			1.2
		
		
			commons-dbcp
			commons-dbcp
			1.4
		

		
			com.alibaba
			fastjson
			1.1.41
		
		
			org.apache.cxf
			apache-cxf
			${cxf.vesion}
			pom
		
		
			org.apache.cxf
			cxf-rt-frontend-jaxws
			${cxf.vesion}
		
		
			org.apache.cxf
			cxf-rt-transports-common
			2.5.4
			jar
			compile
		
		
			org.apache.cxf
			cxf-rt-core
			${cxf.vesion}
			jar
			compile
		

	
	
		springMVCMaven
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				3.1
				
					1.7
					1.7
				
			
		
	
	


2.web页面配置

 

	springMVC
	
		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp
	
	
	
		MVC
		org.springframework.web.servlet.DispatcherServlet
		
		
			
			contextConfigLocation
			
				classpath:mvc.xml
			
		
		1
	

	
	
		MVC
		*.do
	

	
	
		org.springframework.web.context.ContextLoaderListener
	

	
	
		contextConfigLocation
		classpath:root-context.xml
	

	
	
		CXFServlet
		org.apache.cxf.transport.servlet.CXFServlet
		1
	

	
		CXFServlet
		/cxf/*
	


2.mvc配置



	
	
	
	
	
	
	

3.root-context配置



	
	
	
	
	
		
			classpath:db.properties
		
	
	
	
			
		
		
		
		
		
		
		
	
	
	
		
		
	
	
		
	
	
	
	
	
		
	
  	
	
  	
                  implementorClass="ws.HelloService">
    


4.配置sqlMapperConfig和sqlMapper
注:非必须,可不写,若如想配置,转至另一篇博客查看,此处省略,
https://blog.csdn.net/ztcyaonulia/article/details/84547583
5.编写实体类entity.User

package entity;

public class User {
	private int id;
	private String name;
	private String realName;
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", realName=" + realName + "]";
	}
	public User() {
	}
	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 getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
}

6.编写被访问的服务器端webservice

package service.serviceImpl;

import javax.annotation.Resource;
import javax.jws.WebMethod;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Service;
import entity.User;
import ws.HelloService;

@Service
public class HelloServiceImpl implements HelloService{
//此处引入的resource为处理ibatis映射的工具,可省略
	@Resource(name="sqlMapClientTemplate")
	public SqlMapClientTemplate sqlMapClientTemplate;
	//webmethod声明该方法向外公布,可去掉
@WebMethod
//msg为从客户端接受来的msg
	public String sayHello(String msg) {
		return "hello"+msg;
	}
}

7.controller,可测试webservice可否正常调用

package controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import entity.User;
import ws.HelloService;

@Controller
@RequestMapping("/hello.do")
public class hello {
//引入资源为中间工程接口
	@Resource
	public HelloService helloServiceImpl;
	@RequestMapping(params = "action=showIndex")
	public ModelAndView index() {
		User user=new User();
		System.out.println(helloServiceImpl.sayHello(user.getName()));
		ModelAndView mvc=new ModelAndView("hello");
		return mvc;
	}
}

(二)中间工程配置编写
1.中间工程配置文件wsclient





			
	

2.编写接口ws包下的HelloService(该项目为maven项目jar格式)

package ws;
import javax.jws.WebService;
@WebService
public interface HelloService {
	public String sayHello(String name);
}

3.右键项目run as->Maven Install,将项目发布为jar包
(三) .编写客户端配置文件(注:版本配置等等最好与服务器端一样)
1.Maven 的pom依赖



	4.0.0
	ztc
	ws-client
	0.0.1-SNAPSHOT
	war
	springMVCMaven Maven Webapp
	http://maven.apache.org
	
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				3.1
				
					1.7
	
					1.7

				
			
		
	
	
		UTF-8
		3.1.4.RELEASE
		2.2.9
	
	
		
			junit
			junit
			3.8.1
			test
		
		
			ztc
 			ws-api
  			0.0.1-SNAPSHOT
		
		
			com.alibaba
			dubbo
			
				
					org.springframework
					spring
				
			
			2.5.3
		
		
			com.101tec
			zkclient
			0.10
		
		
		
			org.springframework
			spring-context
			${spring.vesion}
		
		
		
			mysql
			mysql-connector-java
			5.1.38
		
		
			org.springframework
			spring-aop
			${spring.vesion}
		
		
			org.springframework
			spring-asm
			${spring.vesion}
		
		
			org.springframework
			spring-aspects
			${spring.vesion}
		
		
			org.springframework
			spring-beans
			${spring.vesion}
		
		
			org.springframework
			spring-context
			${spring.vesion}
		
		
			org.springframework
			spring-context-support
			${spring.vesion}
		
		
			org.springframework
			spring-core
			${spring.vesion}
		
		
			org.springframework
			spring-expression
			${spring.vesion}
		
		
			org.springframework
			spring-instrument
			${spring.vesion}
		
		
			org.springframework
			spring-instrument-tomcat
			${spring.vesion}
		
		
			org.aspectj
			aspectjweaver
			1.6.9
		
		
			commons-pool
			commons-pool
			1.5.3
		
		
			commons-collections
			commons-collections
			3.2
		
		
			org.springframework
			spring-jms
			${spring.vesion}
		
		
			org.springframework
			spring-oxm
			${spring.vesion}
		
		
			org.springframework
			spring-web
			${spring.vesion}
		
		
			org.springframework
			spring-webmvc
			${spring.vesion}
		
		
			org.springframework
			spring-webmvc-portlet
			${spring.vesion}
		
		
			org.springframework
			spring-struts
			${spring.vesion}
		
		
			commons-httpclient
			commons-httpclient
			3.1
		
		
		
			org.slf4j
			slf4j-api
			1.5.10
		
		
			org.slf4j
			slf4j-log4j12
			1.5.10
		
		
			org.apache.ibatis
			ibatis-sqlmap
			2.3.0
		
		
			org.apache.ibatis
			ibatis-core
			3.0
		
		
		
			org.springframework
			spring-orm
			${spring.vesion}
		
		
			org.springframework
			spring-jdbc
			${spring.vesion}
		
		
			org.apache.geronimo.specs
			geronimo-servlet_2.5_spec
			1.2
		
		
			commons-dbcp
			commons-dbcp
			1.4
		

		
			com.alibaba
			fastjson
			1.1.41
		
		
			org.apache.cxf
			apache-cxf
			${cxf.vesion}
			pom
		
		
			org.apache.cxf
			cxf-rt-frontend-jaxws
			${cxf.vesion}
		
		
			org.apache.cxf
			cxf-rt-transports-common
			2.5.4
			jar
			compile
		
		
			org.apache.cxf
			cxf-rt-core
			${cxf.vesion}
			jar
			compile
		
	


2.web配置



	springMVC
	
		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp
	
	
	
		MVC
		org.springframework.web.servlet.DispatcherServlet
		
		
			
			contextConfigLocation
			
				classpath:mvc.xml
			
		
		1
	

	
	
		MVC
		*.do
	
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
		contextConfigLocation
		classpath:root-context.xml
	

3.配置root-context文件




	


4.配置mvc




	
	
	
	
	
	
	

5.编写controller.TestController

package controller;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import ws.HelloService;

@Controller
@RequestMapping("hello.do")
public class TestController {
	@Resource
	public HelloService helloService; 
	@RequestMapping(params="action=testController")
	public void testController() {
		System.out.println(helloService.sayHello("lisi"));
	}
}

三.运行
把客户端和服务端同时发布到tomcat,
运行客户端controller

你可能感兴趣的:(webservice的创建,调用及客户端,服务端分离(使用maven工程))