注明:jersey-2.10.1需要JDK7
1、构建Maven工程,引入相关的依赖
<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>rest</groupId>
<artifactId>rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>rest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
</project>
2、编写启动Jersey服务类
package com.rest.panguoyuan.test;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
public class Server {
private static final URI BASE_URI = URI.create("http://10.71.197.201:8088/basepath/");
public static void main(String[] args) {
try {
<span style="white-space:pre"> </span> System.out.println("\"Hello World\" Jersey Example App");
<span style="white-space:pre"> </span> final ResourceConfig resourceConfig = new ResourceConfig(ServerImpl.class);
<span style="white-space:pre"> </span> final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig);
<span style="white-space:pre"> </span> System.out.println(String.format("Application started.\nTry out %s%s\nHit enter to stop it...",
<span style="white-space:pre"> </span> BASE_URI, "classpath/methodpath/parameters"));
<span style="white-space:pre"> </span> System.in.read();
<span style="white-space:pre"> </span> server.shutdown();
} catch (Exception ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
3、编写提供外部访问的服务类
package com.rest.panguoyuan.test;
import java.io.UnsupportedEncodingException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import com.google.gson.Gson;
@Path("classpath")
public class ServerImpl {
@GET
@Path("methodpath/{userId}")
@Produces("text/plain")
public String getUser(@PathParam("userId") int userId) {
User doc = new User("panguoyuan",90);
Gson gson = new Gson();
String jsonDoc = gson.toJson(doc);
System.out.println("userId="+userId);
return jsonDoc;
}
}
4、相关类(User)
package com.rest.panguoyuan.test;
public class User{
private String name;
private int age;
public User() {}
public User(String name,int age) {this.name = name; this.age = age;}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}