cxf的web service服务,包含client端、service端
生活随笔
收集整理的這篇文章主要介紹了
cxf的web service服务,包含client端、service端
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.下載需要的客戶端、服務端Jar,客戶端jar包鏈接:? https://github.com/zhangliqingyun/jarlist/tree/master/webserviceclient? ? 服務端jar包鏈接:https://github.com/zhangliqingyun/jarlist/tree/master/webserviceservice
2.配置web service服務,實現客戶端與服務端通信
(1)web.xml內容
<display-name>xxfw</display-name><servlet><servlet-name>Way REST Service</servlet-name><servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class><init-param> ?<!—初始化服務à<param-name>jersey.config.server.provider.packages</param-name><param-value>com.rdtsw.service</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet><description></description><servlet-name>MsgSevlet</servlet-name><servlet-class>com.rdtsw.action.MsgSevlet</servlet-class></servlet><servlet-mapping><servlet-name>Way REST Service</servlet-name><url-pattern>/ws/*</url-pattern></servlet-mapping><servlet-mapping><servlet-name>MsgSevlet</servlet-name><url-pattern>/MsgSevlet</url-pattern></servlet-mapping>(2)服務類
@Path("/msg")public?class?MsgService(3)服務器端接收文件代碼
//文件接收@POST@Path("/RecXMLData/{fileName}")@Produces({"application/json;charset=utf-8","text/plain"}) ??public?String receiveFile(@PathParam("fileName") String fileName, @Context?HttpServletRequest request, @Context?HttpServletResponse response) {try?{ ??FileOutputStream fos?= new?FileOutputStream(new?File(path?+ fileName));byte[] buffer?= new?byte[8096];InputStream is?= request.getInputStream();int?len;while?((len?= is.read(buffer)) != -1){fos.write(buffer, 0, len);}fos.flush();fos.close();return?"ok";} catch?(Exception e) {return?"no"; }}(4)服務器端發送文件
//文件發送@POST@Path("/SendXmlData/{fileName}")@Produces({"application/json;charset=utf-8","text/plain"}) ?public?String sendFile(@PathParam("fileName") String fileName, @Context?HttpServletRequest request, @Context?HttpServletResponse response) {try?{File testFile?= new?File(path?+ fileName);FileInputStream fi?= new?FileInputStream(testFile);byte[] buf?= new?byte[2048];DataOutputStream dos?= new?DataOutputStream(response.getOutputStream());dos.writeUTF(testFile.getName());int?len1;while?((len1?= fi.read(buf)) != -1){dos.write(buf, 0, len1);}dos.flush();dos.close();fi.close();return?"200";} catch?(Exception e) {return?"404"; } ??}(5)客戶端發送文件
public?static?void?send(String fileName,String deskUrl){//String filename = "bbb.xml";HttpClient ht?= new?DefaultHttpClient();HttpPost post?= new?HttpPost("http://192.168.0.123:9080/xxfw/ws/msg/SendXmlData/"+fileName);HttpResponse rs?= null;try?{rs?= ht.execute(post);DataInputStream is??= new?DataInputStream(rs.getEntity().getContent());byte[] buffer?= new?byte[2048];int?len2;//讀出文件名String filename?= is.readUTF();FileOutputStream fos?= new?FileOutputStream(new?File(deskUrl+filename)); ?while((len2=is.read(buffer))!=-1){fos.write(buffer, 0, len2);}fos.flush();fos.close();is.close();} catch?(Exception e) {e.printStackTrace();}finally{System.out.println("文件發送成功");post.abort();}}(6)客戶端接收文件
public?static?void?receive(String fileName,String deskUrl) throws?JSONException, URISyntaxException, FileNotFoundException{//String filename = "bbb.xml";Client c?= Client.create(); ?URI u?=new?URI("http://192.168.0.123:9080/xxfw/ws/msg/RecXMLData/"+fileName);WebResource resource?= c.resource(u);MultivaluedMapImpl params?= new?MultivaluedMapImpl();File testFile?= new?File(deskUrl+fileName);FileInputStream fis?= new?FileInputStream(testFile);String aaa?= resource.entity(fis).post(String.class);System.out.println("result:"+aaa);}(7)客戶端傳遞參數
public?static?void?regist() throws?URISyntaxException{Client client?= Client.create(); ?URI u?= new?URI(getUrl("RegJY4GSvr")); ?????WebResource resource?= client.resource(u); ???MultivaluedMapImpl params?= new?MultivaluedMapImpl(); ????//傳遞多個參數的方法 ??params.add("xh", "炫號"); ?params.add("zclx", "注冊類型"); ?params.add("imei", "imei"); ?String result?= resource.queryParams(params).get(String.class); ??System.out.println(result); ??}(8)服務端接收參數??
//注冊服務@GET??@Path("RegJY4GSvr")@Produces({"text/plain"}) ??public?String regist(@QueryParam("xh") String xh,@QueryParam("zclx") String zclx,@QueryParam("imei") String imei,@Context?HttpServletRequest request,@Context?HttpServletResponse response){java.util.Date now?= new?java.util.Date();String registDate?= format.format(now); ?//注冊日全部String zcdwdm?= "000001"; ?//注冊單位代碼map.put("xh", xh);map.put("zclx", zclx);map.put("imei", imei);map.put("zcrq", registDate); ???map.put("zcdwdm", zcdwdm);try?{xxfwDao.insertRegist(map);} catch?(Exception e) {e.printStackTrace();} ?return?"注冊成功"; ?}(9)客戶端接收服務端反回的list集合
public?static?void?init() throws?URISyntaxException{ClientConfig clientConfig?= new?DefaultClientConfig();clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);Client client?= Client.create(clientConfig);MultivaluedMapImpl params?= new?MultivaluedMapImpl();String zcdwdm?="000001";String xxlx?= "xxlx";params.add("zcdwdm", zcdwdm);params.add("xxlx", xxlx);WebResource resource?= client.resource(getUrl("InitJY4GSvr")); ???//得到注冊服務初始化的urlGenericType<List<Demo>> hsp?= new?GenericType<List<Demo>>(){}; ???//返回結果集合類型List<Demo> dwdmList= ?resource.queryParams(params).get(hsp); ?????//單位代碼和油料代碼的集合System.out.println(dwdmList.get(0).getYldmList().get(0).getYldm()); ?(10)服務端返回list集合??
//得到單位代碼@GET@Path("InitJY4GSvr")@Produces({ MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})public?Response InitJY4GSvr(@QueryParam("zcdwdm") String zcdwdm,@QueryParam("xxlx") String xxlx, @Context?HttpServletRequest request, @Context?HttpServletResponse response){map.put("zcdwdm", zcdwdm);map.put("xxlx", xxlx);List<Demo> list?= new?ArrayList<Demo>();try?{List<Dwdm> dwdmList?= xxfwDao.getDwdmList(map); ????????????????????//得到單位代碼集合List<Yldm> yldmList?= xxfwDao.getYldmList(map); ????????????????????//得到油料代碼集合Demo demo?= new?Demo();demo.setDwdmList(dwdmList);demo.setYldmList(yldmList);list.add(demo);GenericEntity?entity?= new?GenericEntity<List<Demo>>(list) {}; ?//返回結果類型return?Response.ok(entity).build();} catch?(Exception e) {e.printStackTrace();}return?null;}?
?
?
?
總結
以上是生活随笔為你收集整理的cxf的web service服务,包含client端、service端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【C】 4个数排列组合
- 下一篇: 著作权保护意见允许当事人通过区块链等方式