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