<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-simple-http</artifactId>
<version>2.25</version>
</dependency>
import java.net.URI;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.simple.SimpleContainerFactory;
public class App {
public static void main(String[] args) throws InterruptedException {
System.out.println("Hello World!");
URI baseUri = UriBuilder.fromUri("http://127.0.0.1/").port(9998).build();
ResourceConfig config = new ResourceConfig(MyResource.class);
SimpleContainerFactory.create(baseUri, config);
}
}
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("myresoure")
public class MyResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getIt() {
return "hello";
}
}
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<version>2.9</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.9</version>
</dependency>
import java.io.IOException;
import java.net.URI;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
public class Jersey1App2 {
// curl -i -s http://127.0.0.1:8899/myapp/mmresource/json
public static void main(String[] args) {
ResourceConfig config = new ResourceConfig().packages("gaofeng.jersey1.resource");
GrizzlyHttpServerFactory.createHttpServer(URI.create("http://127.0.0.1:8899/myapp/"), config);
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/mmresource")
public class MyResource {
@GET @Path("txt")
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
@GET @Path("json")
@Produces(MediaType.APPLICATION_JSON)
public String getJson() {
return "{\"name\":\"Got it!\"}";
}
@POST @Path("desk")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Desk add(Desk desk) {
desk.setAge(desk.getAge()+1);
return desk;
}
}
public class Desk{
private String name;
private int age;
public Desk() {}
public Desk(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;
}
}
import java.io.IOException;
import java.net.URI;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.simple.SimpleContainerFactory;
public class Jersey1App {
//curl -i -s http://127.0.0.1:8899/mmresource/json
public static void main(String[] args) throws IOException {
ResourceConfig config = new ResourceConfig().packages("gaofeng.jersey1.resource");
//myapp没有用,curl时不能有myapp
SimpleContainerFactory.create(URI.create("http://127.0.0.1:8899/myapp/"), config);
}
}