加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

CXF创建webservice服务端、客户端,与spring集成

发布时间:2020-12-16 23:15:04 所属栏目:安全 来源:网络整理
导读:服务端 1.必须jar包:cxf-2.6.1.jar 2.spring-cxf.xml配置文件 ? ?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:jaxws="http://cxf.apache.

服务端

1.必须jar包:cxf-2.6.1.jar

2.spring-cxf.xml配置文件

?

<?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:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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" />

	<bean id="checksystemserviceimp" class="a.rst.checksystem.server.CheckSystemServiceImp">
	</bean>

	<jaxws:endpoint id="webservice" implementor="#checksystemserviceimp"
		address="/webservice">
	</jaxws:endpoint>
</beans>

?

3. web.xml配置spring-cxf.xml

<listener>
   <listener-class>com.chinavvv.util.ContextLoaderListener</listener-class>
 </listener>  
     <context-param>   
        <param-name>contextConfigLocation</param-name>   
        <param-value>/WEB-INF/spring-cxf.xml
        </param-value>

    </context-param>
<!--还需要配置以下servlet-->
<servlet>??? 
??<servlet-name>CXFService</servlet-name>??? 
??<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
??<load-on-startup>1</load-on-startup>? 
?</servlet> 
?<servlet-mapping>??? 
??<servlet-name>CXFService</servlet-name>??? 
??<url-pattern>/service/*</url-pattern>
?</servlet-mapping>


4. java类、接口如下,包含接口CheckSystemService、接口实现类CheckSystemServiceImp

package a.rst.checksystem.server;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface CheckSystemService {
 public void test(@WebParam(name="param") String param);
 public String checkWebSer(String aac002,String aac003,String aab301) throws Exception;
}



?

@WebService(endpointInterface = "a.rst.checksystem.server.CheckSystemService")
public class CheckSystemServiceImp implements CheckSystemService {

public void test(String param) {
  System.out.println("001webservice is success ");
  System.out.println("002dao is annotationed? "+relectdao);
  System.out.println("003url param:"+param);

}

public String checkWebSer(String aac002,String aab301)
   throws Exception {

Ceshibean bean=new CeshiBean();//这里是自定义的Javabean

return jsontToString(PersonalInfo);// 返回类型设置为json格式的字符串,没有使用Bean

}

 
private String jsontToString(Object o){
  JsonConfig jsonConfig = new JsonConfig();  //建立配置文件
   jsonConfig.setIgnoreDefaultExcludes(false);  //设置默认忽略
    jsonConfig.setExcludes(new String[]{"child","multipartRequestHandler","resultValueMap","servletWrapper","validatorResults"}); 
   //net.sf.json.JSONObject jsonbean = net.sf.json.JSONObject.fromObject(bean,jsonConfig);
   JSONObject json = JSONObject.fromObject(o,jsonConfig);
  return json.toString();
 }

}


?

测试是否发布成功地址:http://127.0.0.1:8080/service/webservice?wsdl

?

客户端:

cd d:/java/bin

wsimport - keep -p com.webservice.client http://127.0.0.1:8080/service/webservice?wsdl

用Java api生成客户端包,以上命令将在d:/java/bin目录下生成com.webservice.client包。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读