• 用jersey写 java restfull web services 输出xml格式数据


     1 logic

    package com.toic.rest;
    
    import com.toic.model.Folder;
    
    import java.util.logging.Logger;
    
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;
    
    @Path("/folders")
    public class RESTService {
    
        private Logger logger = Logger.getLogger(RESTService.class.getName());
    
        public RESTService() {
            super();
        }
        // 这个 成功  http://localhost:7001/RESTfull001/folders/sayhello/li
        @GET
        @Path("sayhello/{name}")
        @Produces("text/plain")
        public String removeStudent(@PathParam("name") String name) {
            String say = name + " said hello!";
            return say;
        }
    
         //http://localhost:7001/RESTfull001/folders/guid/000234
        @GET
        @Path("/guid/{folderguid}")
        //@Produces({MediaType.APPLICATION_XML })
        @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
        
        public Folder getFoldersByGUID(@PathParam("folderguid") String folderguid) {
    
            
    //        第一种    
            Folder folder = Folder.makeFolders();
            
            if(!folderguid.equals("0")){
                
                for(int i=0;i<folder.getChildFolders().size();i++){
                    
                    if(((Folder)folder.getChildFolders().get(i)).getFolderGUID().equals(folderguid)){
                        
                        folder=(Folder)folder.getChildFolders().get(i);
                    }
                    
                }
                
            }    
            return folder;
            
            
    //    第二种    
    //        Folder rootFolder = new Folder();        
    //        rootFolder.setFolderName("Root Folder");
    //        rootFolder.setOwner("juan");
    //        rootFolder.setFolderGUID("000234");
    //        rootFolder.setFolderType("NATIVE");
    //        return rootFolder;
            
            
            ////return Response.status(Response.Status.OK).entity(folder).build();
        }
        
        public static void main(String s[]){
            System.out.println(new RESTService().getFoldersByGUID("00") );
    //调用方法

    //或者在浏览器输入URL
    http://localhost:7001/RESTfull001/folders/guid/000234

    //或者下面在java代码中调用

    /////////////////////////////////////////////////////////
    // //需要导入下面的lib中jar OracleMiddlewareOracle_Homewlserverserverlib
    // System.out.println("**** ****");
    // Client client = ClientBuilder.newClient().register(JacksonJsonProvider.class);// 注册json 支持
    // WebTarget target = client.target(url + "/guid/000234");
    // Response response = target.request().get();
    // Folder folder = response.readEntity(Folder.class);
    // System.out.println(folder.getFolderName()+" "+folder.getFolderGUID()+" "+folder.getFolderType()+" "+folder.getOwner());
    // response.close();
    //
    // //打印
    // //**** ****
    // // Root Folder 000234 NATIVE juan
    /////////////////////////////////////////////////////////

    //或者如下调用

    //com.sun.jersey.api.client.Client client = com.sun.jersey.api.client.Client .create();

    //Folder folder =client.resource(url + "/guid/" + "200234").accept(MediaType.APPLICATION_XML_TYPE).get(Folder.class);

    //System.out.println(folder.getFolderGUID()+" "+folder.getFolderName()+" "+folder.getFolderType()+" "+folder.getOwner());
    //200234 Folder2 NATIVE xiaolu

         


    } }

    2  model

    package com.toic.model;
     
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement  
    public class Folder {
        
        private String folderName;
        
        private String owner;
        
        private String folderGUID;
        
        private String folderType;
        
        private List childFolders = new ArrayList();
        
        
        public Folder() {
            super();
        }
        public String getFolderName() {
            return folderName;
        }
        public void setFolderName(String folderName) {
            this.folderName = folderName;
        }
        public String getOwner() {
            return owner;
        }
        public void setOwner(String owner) {
            this.owner = owner;
        }
        public String getFolderGUID() {
            return folderGUID;
        }
        public void setFolderGUID(String folderGUID) {
            this.folderGUID = folderGUID;
        }
        public String getFolderType() {
            return folderType;
        }
        public void setFolderType(String folderType) {
            this.folderType = folderType;
        }
        public List getChildFolders() {
            return childFolders;
        }
        public void addChildFolder(Folder childFolder) {
            this.childFolders.add(childFolder);
        }
        
        public static Folder makeFolders() {
            
            Folder rootFolder = new Folder();        
            rootFolder.setFolderName("Root Folder");
            rootFolder.setOwner("juan");
            rootFolder.setFolderGUID("000234");
            rootFolder.setFolderType("NATIVE");
            
            Folder childFolder1 = new Folder();
            
            childFolder1.setFolderName("Folder1");
            childFolder1.setOwner("yang");
            childFolder1.setFolderGUID("100234");
            childFolder1.setFolderType("NATIVE");
            
            Folder childFolder2 = new Folder();
            
            childFolder2.setFolderName("Folder2");
            childFolder2.setOwner("xiaolu");
            childFolder2.setFolderGUID("200234");
            childFolder2.setFolderType("NATIVE");
            
            rootFolder.addChildFolder(childFolder1);
            
            rootFolder.addChildFolder(childFolder2);
            
            return rootFolder;
        }
    }

    3 web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>oracle.eclipse.tools.jsffacet</display-name>
      
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
      <servlet>
        <servlet-name>jersey</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>jersey</servlet-name>
        <url-pattern>*</url-pattern>
      </servlet-mapping>
    </web-app>
  • 相关阅读:
    上传图片,语音,和富文本(webuploader,dropzone, froala)
    java代码备份mysql数据库
    maven 父子工程打包 并且上传linux服务器
    docker+fastdfs+nginx 实现分布式大文件存储系统以及视频缓存播放
    docker eureka 注册中心 服务提供者通信机制
    lvs dr 模型配置详解
    spring cloud 详解
    JS前端创建CSV或Excel文件并浏览器导出下载
    修改ElementUI源码实践
    Leaflet+heatmap实现离线地图加载和热力图应用
  • 原文地址:https://www.cnblogs.com/rojas/p/4626000.html
Copyright © 2020-2023  润新知