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>