ssh/ssm使用CXF实现基于Rest方式的WebService并返回json数据

使用CXF实现基于Rest方式的WebService

1.需要的jar包:

ssh/ssm使用CXF实现基于Rest方式的WebService并返回json数据_第1张图片

2.applicationContext-webservice.xml配置文件




	

	
	
	 	
	

		
			
		
		
  		  
              
             
          

		
			
			
		
	
		


3.web.xml配置:主要分2部分,读取相关xml文件与配置CXFService


    org.apache.tiles.extras.complete.CompleteAutoloadTilesListener
  
  
    contextConfigLocation
    
		/WEB-INF/applicationContext-*.xml
		
  
 
    CXFService
    org.apache.cxf.transport.servlet.CXFServlet
  
  
    CXFService
    /webservice/*
  



4.webservice接口:ILakeTestWebService
package webservice.shj.Interfaces;


import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
@WebService(targetNamespace="LakeWebService")
@Path(value = "")  
public interface ILakeTestWebService {
	@GET  
	@Path(value = "/LakeList")
	//查询湖库信息
	public List LakeList(@QueryParam("wc") @WebParam(name = "waterCondition")String waterCondition);
}
4.webservice实现类:LakeWebServiceImpl.

此处只是hibernate的一个查询,dao层不再展示

package webservice.shj.Impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.shj.daoImpl.WebServiceDao;

import base.BaseWSSupport;
import vo.Lake;
import vo.Log;
import webservice.shj.Interfaces.ILakeTestWebService;

@Component
@Transactional
@WebService(targetNamespace="LakeWebService")
public class LakeWebServiceImpl extends BaseWSSupport implements ILakeTestWebService  {
	
	@Resource(name="WebServiceDao")
	private WebServiceDao webServiceDao;
	private GetHql getSql=new GetHql();
	
	
	@Override
	//查询湖库信息
	public List LakeList(String waterCondition) {
		// TODO Auto-generated method stub
		StringBuffer hql=new StringBuffer(" from Lake p where ");
		String hql1=getSql.gethql2(hql,"Lake","waterCondition",waterCondition);
		String methodname = Thread.currentThread() .getStackTrace()[1].getMethodName();
		List list;
		int a=0;
		String errMsg="";
		try {
			list = webServiceDao.LakeList(hql1);
			a=1;
			errMsg="json数据获取成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Timestamp time = new Timestamp(System.currentTimeMillis());
			Log lg=new Log(time,methodname,e.toString());
			webServiceDao.saveLog(lg);
			a=0;
			errMsg="方法:"+methodname+"异常;"+e.toString();
			list=null;
		}
		Map map = new LinkedHashMap();
		map.put("okFlag",a);
		map.put("errMsg",errMsg);
		map.put("data", list);
		List alist = new ArrayList();
		alist.add(map);
		return alist;
	}
}




5.启动项目,访问地址:http://localhost:8080/项目/webservice/rest/LakeList.json

ssh/ssm使用CXF实现基于Rest方式的WebService并返回json数据_第2张图片


   

   


你可能感兴趣的:(java,cxf,webservice,web)