记录自己的菜鸟成长之路...
1.使用cxf技术与spring结合,工程使用mybatis+struts2
2.前后台分离,接口写在前端工程里
第一次做是跟着http://my.oschina.net/aptx4869/blog/293044?fromerr=Kzidifd5一步步走的
服务端:
一:首先导入jar包,工程内部原有spring的jar包,这里只导入cxf需要jar包
jar包添加入maven仓库,可以再http://mvnrepository.com/artifact/commons-cli/commons-cli/1.2网站中自动生成pom配置代码
pom配置文件如下:
<!-- cxf -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.14</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.7.14</version>
</dependency>
二:创建一个服务接口
package com.zyzx.approval.webservice;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface IAuthServerService {
public String checkupInterface(
@WebParam(name = "applicantId") String applicantId,
@WebParam(name = "spType") String spType,
@WebParam(name = "moduleCode") String moduleCode,
@WebParam(name = "createTime") String createTime,
@WebParam(name = "spName") String spName,
@WebParam(name = "spState") String spState,
@WebParam(name = "spId") String spId);
}
三:接口实现类:
/**
* @Title: package-info.java
* @Package com.zyzx.report.webservice.impl
* @author zhoubin
* @version V1.0
*/
/**
* @ClassName: package-info
* @author zhoubin
*/
package com.zyzx.approval.webservice.impl;
import java.util.HashMap;
import java.util.Map;
import javax.jws.WebService;
import javax.servlet.http.HttpServlet;
import org.apache.cxf.feature.Features;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Transactional;
import com.ai.frame.bean.InputObject;
import com.ai.frame.bean.OutputObject;
import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;
import com.zyzx.approval.webservice.IAuthServerService;
@Transactional
@WebService(endpointInterface = "com.zyzx.approval.webservice.IAuthServerService")
@Features(features = "org.apache.cxf.feature.LoggingFeature")
public class AuthServerServiceImpl extends HttpServlet implements IAuthServerService{
private static final long serialVersionUID = 215925817441980210L;
// private Logger logger = Logger.getLogger(AuthServerServiceImpl.class);
private static final Logger logger = LoggerFactory.getApplicationLog(AuthServerServiceImpl.class);
public String checkupInterface(String applicantId, String spType,
String moduleCode, String createTime, String spName,
String spState, String spId) {
logger.info("开始调用:webservice服务,","AuthServerServiceImpl--checkupInterface:Start");
OutputObject outputObject = null;
InputObject inputObject = null;
try{
//瀹氫箟鍏ュ弬map
Map<String, String> inputParamMap = new HashMap<String, String>();
inputParamMap.put("applicantId", applicantId);
inputParamMap.put("spType", spType);
inputParamMap.put("moduleCode", moduleCode);
inputParamMap.put("createTime", createTime);
inputParamMap.put("spName", spName);
inputParamMap.put("spState", spState);
inputParamMap.put("spId", spId);
inputObject = new InputObject("checkupService","getNewsToDB",inputParamMap);
outputObject = getControlService().execute(inputObject);
} catch (Exception e){
logger.error("调用出错:webservice服务,","AuthServerServiceImpl--checkupInterface:error" + e.getMessage(), e);
}
logger.info("结束调用:webservice服务,","AuthServerServiceImpl--checkupInterface:End");
return outputObject.getReturnMessage();
}
private com.zyzx.approval.control.IControlService getControlService(){
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/spring-services.xml","classpath:spring/spring-dubbo-consumer.xml");
com.zyzx.approval.control.IControlService controlService = (com.zyzx.approval.control.IControlService)ac.getBean("controlService");
return controlService;
}
}
四:在webservice.xml配置文件中配置spring,在spring容器中加入已写好的服务类
<?xml version="1.0" encoding="UTF-8"?>
<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:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.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:endpoint id="IAuthServerService" implementor="com.zyzx.approval.webservice.impl.AuthServerServiceImpl" address="/Checkup" />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/system.properties</value>
</list>
</property>
</bean>
<bean id="controlService" class="com.zyzx.approval.control.impl.ControlServiceImpl">
<property name="papControlService" ref="papControlService" />
</bean>
<!-- 接口服务 -->
<bean id="AuthServerServiceImpl" class="com.zyzx.approval.webservice.impl.AuthServerServiceImpl"></bean>
</beans>
五:在web.xml中添加加载spring配置的配置文件 以及在web.xml中加入cxf servlet:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</context-param>
<!-- cxf servlet -->
<servlet>
<display-name>CXF Servlet</display-name>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webService/*</url-pattern>
</servlet-mapping>
(注意:写cxf servlet配置文件,也就是下面这段
<url-pattern>/webService/*</url-pattern>
代码时,映射路径不要写成/*,否则会出现访问不了项目主页的情况,可以我写的那样或者别的项目中没有使用过的路径来作为cxf servlet的请求路径。)
(首先注意,我的webService中S是大写的,其次8090,嘿嘿,tomcat默认端口我给换了)
下面是成功的样子:
客户端:(自动生成)
测试的时候我是用myeclipse逆向生成的客户端代码,这里也总结一下,因为我用的是myeclipse2015,网上没查到2015对应反向生成webservice客户端的例子,其他版本的myeclipse多少有点不一样
一:首先创建一个java工程(javaWeb工程不可以,不要问我为什么)
起个名字直接完成,反正测试用的,嘿嘿,
二:然后右键工程→new→other:
三:找到Web Services→Web Service Client,双击进入:
四:输入服务端的访问路径http://localhost:8090/eaa/webService/Checkup?wsdl,这里我已经输入了了:
(不过要注意:图片右下面的配置是可以修改的,所属工程名啦什么的,不过一般都默认了)
五:直接完成,可以看到系统自动生成的是这么些个文件:
六:所以蛋疼的来了,貌似没有测试main方法,得嘞 自个儿写一个吧,新建一个class,放哪儿都成:
package com.zyzx.approval.webservice.impl;
import java.rmi.RemoteException;
import com.zyzx.approval.webservice.IAuthServerService;
import com.zyzx.approval.webservice.IAuthServerServiceProxy;
public class test {
public static void main(String[] args) {
IAuthServerService s = new IAuthServerServiceProxy();
try {
String cc= s.checkupInterface("7123","1","ID0001","20160315145732","666","0","888");
System.out.println(cc);
System.out.println("111");
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
接下来运行main方法测试下就成了,右键→Run As →Java Application:
下面是返回的结果:警告神马的,不要放在心上
客户端:
一: