码迷,mamicode.com
首页 > Web开发 > 详细

关于webservice实现web接口

时间:2014-10-08 17:57:05      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   java   

package service;

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
* @description 功能描述:
* @author 作 者: 周志伟
* @param 参 数:
* @createdate 建立日期: 2014-9-4上午9:37:40
* @projectname 项目名称: spring_mvctype
* @packageclass 包及类名: com.spring.mvc.service.connservice.java
*/

//第一步首先创建web接口
@WebService
public interface connservice {

@WebMethod
public String Stringlist();

@WebMethod
public List getlist();

}

 

//第二步去实现这个接口

package serviceimpl;

import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import service.connservice;

import com.spring.mvc.jdbc.JdbcDao;

/**
* @description 功能描述:
* @author 作 者: 周志伟
* @param 参 数:
* @createdate 建立日期: 2014-9-4上午9:38:31
* @projectname 项目名称: spring_mvctype
* @packageclass 包及类名: com.spring.mvc.serviceimpl.connserviceimpl.java
*/
@WebService(endpointInterface="service.connservice")
@Service(value="connservice")
public class connserviceimpl implements connservice{




@Autowired
JdbcDao dao;
@Override
/**
* @description 功能描述: webservice 返回list json 结果
* @author 作 者: 周志伟
* @param 参 数:
* @createdate 建立日期: 2014-9-4上午9:38:31
* @projectname 项目名称: spring_mvctype
* @packageclass 包及类名: com.spring.mvc.serviceimpl.connserviceimpl.java
*/
public String Stringlist() {
String sql ="select * from CS_CONTRACT t where t.id=‘30053‘";
List list = dao.queryData(sql); // 执行sql


net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(list); //将结果集转为List json 串

return jsonArray.toString(); //转为String类型
}




/**
* @description 功能描述: webservice 返回list 结果
* @author 作 者: 周志伟
* @param 参 数:
* @createdate 建立日期: 2014-9-4上午9:38:31
* @projectname 项目名称: spring_mvctype
* @packageclass 包及类名: com.spring.mvc.serviceimpl.connserviceimpl.java
*/
public List getlist() {

String sql ="select * from CS_CONTRACT t where t.id=‘30053‘";
List list = dao.queryData(sql); //执行sql
List weblist=null;
for (int i = 0; i < list.size(); i++) { //结果集是两层List 所以得循环一层List
weblist = (List) list.get(i);
}

return weblist; //返回结果List
}



}

 

 

 

 

第三步添加server-ws.xml webservice配置文件 不过得导入spring  cxf jar包 

 

 

<?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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xmlns:simple="http://cxf.apache.org/simple"
xsi:schemaLocation="http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/simple
http://cxf.apache.org/schemas/simple.xsd"
default-autowire="byName" default-lazy-init="true">

<!-- 导入CXF初始配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />




<!--测试web service 接口 -->

<!--此路径为类的路径 -->
<bean id="borrowere" class="serviceimpl.connserviceimpl" />
<jaxws:endpoint id="webser" implementor="#borrowere" address="/webser" />


</beans>

 

第四步在applicationContext.xml 导入 server-ws.xml 

<!-- webservice 配置文件 -->
<import resource="server-ws.xml"/>

 

第五步在web.xml配置webservice

 

<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

如果有maven 私服可以直接下载jar包

<dependency>
<groupId>org.apache.servicemix.samples.cxf-wsdl-first</groupId>
<artifactId>wsdl-first-cxf-sa</artifactId>
<version>3.3.2</version>
<type>zip</type>
</dependency>

 

第六步发布项目测试 如果出了此 页面证明你的webservice接口已经配置成功 如果别人要调你的接口你只需把服务端url给他即可。http://127.0.0.1:9090/spring_mvc/services/webser?wsdl 可以在他本地直接生成客户端接口进行调用

 

 浏览器输入   http://127.0.0.1:9090/spring_mvc/services/webser?wsdl  //webser 为接口名字自定义

bubuko.com,布布扣

第七步调用接口 新建项目

bubuko.com,布布扣 

 

bubuko.com,布布扣

 

bubuko.com,布布扣

 

bubuko.com,布布扣

 

 

 

 

bubuko.com,布布扣

 

 

 

 

 

 

 

 

 

bubuko.com,布布扣

就这样webservice接口就轻松搞定。。。

关于webservice实现web接口

标签:des   style   blog   http   color   io   os   ar   java   

原文地址:http://www.cnblogs.com/yy123/p/4011242.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!