Jersey应用
来源:互联网 发布:许继电气怎么样知乎 编辑:程序博客网 时间:2024/05/29 18:50
1.pom.xml依赖
<dependency><groupId>com.sun.jersey</groupId><artifactId>jersey-server</artifactId><version>1.19</version></dependency><dependency><groupId>com.sun.jersey</groupId><artifactId>jersey-grizzly2</artifactId><version>1.19</version></dependency><dependency><groupId>com.sun.jersey</groupId><artifactId>jersey-bundle</artifactId><version>1.19</version></dependency><dependency><groupId>com.sun.jersey</groupId><artifactId>jersey-servlet</artifactId><version>1.19</version></dependency><!-- Json --><dependency><groupId>com.sun.jersey</groupId><artifactId>jersey-json</artifactId><version>1.18</version></dependency><dependency><groupId>com.owlike</groupId><artifactId>genson</artifactId><version>0.99</version></dependency>
<servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.ecs.jersey</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
3.server端代码
package com.ecs.jersey;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.ws.rs.Consumes;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import com.ecs.model.User;@Path(value = "/getUsers")public class UserResource {@POST@Consumes(MediaType.APPLICATION_JSON)@Produces(MediaType.APPLICATION_JSON)public Map<String, Object> getUsers() {User user = new User();user.setId(11L);user.setName("张三");Map<String, Object> map = new HashMap<String, Object>();List<User> list = new ArrayList<User>();User u1 = new User();u1.setId(1L);u1.setName("张三");User u2 = new User();u2.setId(2L);u2.setName("李四");list.add(u1);list.add(u2);map.put("list", list);map.put("total", list.size());return map;}}
import java.util.HashMap;import javax.ws.rs.core.MediaType;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONException;import com.alibaba.fastjson.JSONObject;import com.sun.jersey.api.client.Client;import com.sun.jersey.api.client.ClientResponse;import com.sun.jersey.api.client.WebResource;import com.sun.jersey.api.client.config.ClientConfig;import com.sun.jersey.api.client.config.DefaultClientConfig;//jersey客户端调用public class Test {public static void main(String[] args) {ClientConfig cc = new DefaultClientConfig(); Client client = Client.create(cc); WebResource resource = client.resource("http://localhost:8080/rest/getUsers"); JSONObject req = new JSONObject(); try { req.put("query", "name"); } catch (JSONException e) { e.printStackTrace(); } ClientResponse response = resource .accept(MediaType.APPLICATION_JSON) .type(MediaType.APPLICATION_JSON) .post(ClientResponse.class, req); HashMap resp = response.getEntity(HashMap.class); System.out.println(JSON.toJSONString(resp)); }}
控制台输出结果:
{"total":2,"list":[{"id":1,"name":"张三"},{"id":2,"name":"李四"}]}
0 0
- Jersey应用
- jersey
- Jersey
- jersey
- jersey
- Jersey
- jersey
- Jersey
- ANGULAR SPA JERSEY SPRINGMVC 整合应用
- 使用Jersey实现应用服务器和图片服务器分离
- 使用jersey实现应用服务器和图片服务器分离
- 使用Jersey实现应用服务器和图片服务器分离
- 使用jersey实现应用服务器和图片服务器分离
- 使用jersey实现应用服务器和图片服务器分离
- 使用Jersey实现应用服务器和图片服务器分离
- JAVA基础应用——调用Jersey服务
- JBOSS AS7 数据源配置一条龙+jersey 发布应用关键点
- 网上销售平台--jersey实现应用服务器和图片服务器分离(六)
- 100天土鸡饲养计划(82)
- R语言技巧:读取excel文件
- 安卓 bitmap的对应处理 防止oom
- Android N 新特性
- JSP 中文乱码问题
- Jersey应用
- java.net.ConnectException: Connection timed out: connect原因及解决 java设置代理http请求
- Effective Java 2.0_中文版_Item 6
- MBR+BIOS与GPT+UEFI启动系统的区别
- Understanding LSTM Networks
- oracle group by 字段合并
- 更新雅克 git新工程后
- 心灵鸡汤(一)
- [JZOJ4870]涂色游戏