REST实现(Spring下实现+JDK6机制实现)

1、Spring下实现

见:Spring4搭建+REST在Spring上搭建 http://blog.csdn.net/textboy/article/details/51141436


2、利用JDK 6机制实现

这种方式更为灵活,可以设置最大接受请求数和线程数(在Spring下...还真不知在哪里可以设这些参数)

httpServer、httpHandler

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.spi.HttpServerProvider;

/*
 * http://sunnylocus.iteye.com/blog/460945
 * http://www.360doc.com/content/12/0528/17/2569758_214300573.shtml
 * http://outofmemory.cn/code-snippet/35680/Java-HTTP-fuwuqi-example
 * 异步AIO:http://blog.csdn.net/zhongweijian/article/details/8005444
 */
public class MyHttpServerJavaEE6 {
	//启动服务,监听来自客户端的请求
    public static void httpserverService() throws IOException {
        HttpServerProvider provider = HttpServerProvider.provider();
        HttpServer httpserver =provider.createHttpServer(new InetSocketAddress(6666), 100);//监听端口6666,能同时接 受100个请求
        httpserver.createContext("/myApp", new MyHttpHandler());
        //httpserver.setExecutor(null); //使用单线程
        httpserver.setExecutor(Executors.newFixedThreadPool(5));
        httpserver.start();
        System.out.println("server started");
    }
    //Http请求处理类
    static class MyHttpHandler implements HttpHandler {
        public void handle(HttpExchange httpExchange) throws IOException {
            String responseMsg = "ok";   //响应信息
            
            InputStream in = httpExchange.getRequestBody(); //获得输入流
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String temp = null;
            while((temp = reader.readLine()) != null) {
                System.out.println("client request:"+temp);
            }
            
            httpExchange.sendResponseHeaders(200, responseMsg.length()); //设置响应头属性及响应信息的长度
            OutputStream out = httpExchange.getResponseBody();  //获得输出流
            out.write(responseMsg.getBytes());
            out.flush();
            httpExchange.close();
        }
    }
    
    public static void main(String[] args) throws IOException {
        httpserverService();
    }
}

httpClient

import java.net.URI;

import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;


public class SpringRestTestClient {
//	public static final String REST_SERVICE_URI = "http://localhost:8080/springPoC1/service";
	public static final String REST_SERVICE_URI = "http://localhost:6666/myApp";
	
	private static void listAllUsers(){
		RestTemplate restTemplate = new RestTemplate();
		String jsonParam = "{\"name\":\"Aiya\"}";
		try {
			URI uri = restTemplate.postForLocation(REST_SERVICE_URI+"/user1", jsonParam, String.class);
			if (null != uri){
				System.out.println("Location : "+uri.toASCIIString());
			} else {
				System.out.println("uri is null~");
			}
			String result = restTemplate.postForObject(REST_SERVICE_URI+"/user1", jsonParam, String.class);
			System.out.println("testUser~" + result);
		} catch (RestClientException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		listAllUsers();
	}
}

参考:
Python tornado 非阻塞式 Web 服务器框架 - http://www.tornadoweb.cn/documentation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值