jersey

		<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);

	}
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值