REST实现(Spring下实现+JDK6机制实现)
来源:互联网 发布:sql developer输出数据 编辑:程序博客网 时间:2024/05/22 15:28
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
- REST实现(Spring下实现+JDK6机制实现)
- REST(Jersey实现)统一异常机制
- Spring AOP 实现机制
- Spring AOP 实现机制
- spring aop 实现机制
- Spring AOP 实现机制
- Spring AOP 实现机制
- Spring AOP 实现机制
- Jersey+Spring 实现rest 接口 服务调用
- Jersey+Spring 实现rest 接口 服务调用
- Jboss在 jdk6下的 webservice实现冲突解决
- Jboss在 jdk6下的 webservice实现冲突解决
- JDK6中AtomicInteger的实现
- Spring学习之旅(十) Spring MVC实现REST
- 【转】Spring+Spring Security+OAuth2实现REST API权限控制
- Spring+Spring Security+OAuth2实现REST API权限控制
- spring IOC 机制模拟实现
- Spring AOP的实现机制
- ABAP编程 选择屏幕字段权限检查--公司代码事例
- controller-release
- CloudFoundry in 1 Box简介:PCF-Dev篇
- 设计模式简要总结——创建型模式
- BZOJ4520: [Cqoi2016]K远点对
- REST实现(Spring下实现+JDK6机制实现)
- 无监督学习
- Linux内核学习笔记七——内核同步机制和实现方式
- 在Eclipse中使用JUnit4进行单元测试(初级篇)
- Winsock协议配置问题导致网络连接出问题
- Android Studio Unsupported major.minor version 51.0
- position:fixed;实现底层页面不跟随浮层模块滚动
- VC的MDI中实现动态切换文档视图
- GeoTeric地质油藏描述软件图形工作站方案