2017年4月18日 | Leave a comment 问题: 简单实现 javase app 提供RESTful 服务 解决: 使用jersey 可以实现RESTful 服务 http服务程序有jetty / netty /jdk simple http server 多种选择 按下列代码创建好项目编译后 当作普通的 java 程序启动就能调用RESTful 服务了 示例RESTful代码的网址为 http://localhost:6666/product/1 代码: maven pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>me.yek</groupId> <artifactId>jersey1</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-jdk-http</artifactId> <version>2.25.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.25.1</version> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> </project> 12345678910111213141516171819202122232425262728293031323334353637 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>me.yek</groupId> <artifactId>jersey1</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-jdk-http</artifactId> <version>2.25.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.25.1</version> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties></project> 启动代码 App1.java import com.sun.net.httpserver.HttpServer; import java.net.URI; import javax.ws.rs.core.UriBuilder; import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory; import org.glassfish.jersey.server.ResourceConfig; /** * * @author Administrator */ public class App1 { public static void main(String[] argv) { URI uri = UriBuilder.fromUri("http://localhost/").port(6666).build(); ResourceConfig cfg = ResourceConfig.forApplication(new Resource()); HttpServer s = JdkHttpServerFactory.createHttpServer(uri, cfg); //did start s.start(); //Server s = JettyHttpContainerFactory.createServer(baseUri, config); System.out.println("end"); } } 123456789101112131415161718192021 import com.sun.net.httpserver.HttpServer;import java.net.URI;import javax.ws.rs.core.UriBuilder;import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;import org.glassfish.jersey.server.ResourceConfig; /** * * @author Administrator */public class App1 { public static void main(String[] argv) { URI uri = UriBuilder.fromUri("http://localhost/").port(6666).build(); ResourceConfig cfg = ResourceConfig.forApplication(new Resource()); HttpServer s = JdkHttpServerFactory.createHttpServer(uri, cfg); //did start s.start(); //Server s = JettyHttpContainerFactory.createServer(baseUri, config); System.out.println("end"); }} Resource.java import java.util.HashSet; import java.util.Set; import javax.ws.rs.core.Application; /** * * @author Administrator */ public class Resource extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> ret = new HashSet<>(); ret.add(ProductFacade.class); return ret; } } 123456789101112131415161718 import java.util.HashSet;import java.util.Set;import javax.ws.rs.core.Application; /** * * @author Administrator */public class Resource extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> ret = new HashSet<>(); ret.add(ProductFacade.class); return ret; } } 服务提供代码ProductFacade.java: import java.util.HashMap; import java.util.Map; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; /** * * @author Administrator */ @Path("product") public class ProductFacade { @GET @Path("{id}") @Produces("application/json") public Map<String, Object> getProduct(@PathParam("id") final int id) { Map<String, Object> ret = new HashMap<>(); ret.put("title", "范德萨发;了的萨芬 " + id); ret.put("id", id); return ret; } } 123456789101112131415161718192021222324 import java.util.HashMap;import java.util.Map;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces; /** * * @author Administrator */@Path("product")public class ProductFacade { @GET @Path("{id}") @Produces("application/json") public Map<String, Object> getProduct(@PathParam("id") final int id) { Map<String, Object> ret = new HashMap<>(); ret.put("title", "范德萨发;了的萨芬 " + id); ret.put("id", id); return ret; }} 参考: https://jersey.java.net/documentation/latest/deployment.html#deployment.javase