REST实现(Spring下实现+JDK6机制实现)
2016-04-15 15:42
621 查看
1、Spring下实现
见:Spring4搭建+REST在Spring上搭建 http://blog.csdn.net/textboy/article/details/51141436
2、利用JDK 6机制实现
这种方式更为灵活,可以设置最大接受请求数和线程数(在Spring下...还真不知在哪里可以设这些参数)
httpServer、httpHandler
httpClient
参考:
Python tornado 非阻塞式 Web 服务器框架 - http://www.tornadoweb.cn/documentation
见: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
相关文章推荐
- Struts中防止表单重复提交
- java常用sql处理函数类
- HBASE遇到的java.lang.OutOfMemoryError: unable to create new native thread解决方法
- 在Eclipse中使用JUnit4进行单元测试(初级篇)
- java类代码执行顺序
- Java中集合类的关系图谱
- JAVA+ffmpeg+mencoder转换视频
- Gaining Access to the Spring Context in Non Spring Managed Classes
- java poi下载地址
- Windows7部署Android开发环境傻瓜式教程(Eclipse+ADT) (转)
- Java 数据存储
- Java中接口和抽象类的区别及使用的场合
- java thrift返回List<String>异常
- 安装JDK
- 共同学习Java源码--常用数据类型--String(十五)
- Struts2图片的上传下载
- Spring Boot使用自定义的properties
- java list排序
- Spring @PropertySource example
- 浅谈Java回调机制的简单理解