在JAX-RS中使用jackson去处理json,例子如下。下文讲解了,如何将一个对象转变为JSON对象,使用的是jackson。
1 放置resteasy-jackson-provider.jar
2
一个简单对象
Java代码
1.
2. public class
3.
4. String name;
5. int
6.
7. public
8. return
9. }
10.
11. public void
12. this.name = name;
13. }
14.
15. public int
16. return
17. }
18.
19. public void setQty(int
20. this.qty = qty;
21. }
22.
23. }
public class Product {
String name;
int qty;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
}
3 使用注解@Produces("application/json").就可以转换JSON了
Java代码
1. import
2. import
3. import
4. import
5. import
6. import
7.
8. @Path("/json/product")
9. public class
10.
11. @GET
12. @Path("/get")
13. @Produces("application/json")
14. public
15.
16. new
17. "iPad 3");
18. 999);
19.
20. return
21.
22. }
23.
24. @POST
25. @Path("/post")
26. @Consumes("application/json")
27. public
28.
29. "Product created : "
30. return Response.status(201).entity(result).build();
31.
32. }
33.
34. }
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
@Path("/json/product")
public class JSONService {
@GET
@Path("/get")
@Produces("application/json")
public Product getProductInJSON() {
Product product = new Product();
product.setName("iPad 3");
product.setQty(999);
return product;
}
@POST
@Path("/post")
@Consumes("application/json")
public Response createProductInJSON(Product product) {
String result = "Product created : " + product;
return Response.status(201).entity(result).build();
}
}
注意,要把web.xml中的自动扫描注释掉,否则会出错:
<!-- disabled auto scan
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param> -->
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.mkyong.rest.JSONService</param-value>
</context-param>
4 客户端调用服务端的GET,用于将服务端的对象转为JSON,如下:
Java代码
1. try
2.
3. new
4. "http://localhost:8085/Resetjason/json/product/get");
5. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
6. "GET");
7. "Accept", "application/json");
8.
9. if (conn.getResponseCode() != 200) {
10. throw new RuntimeException("Failed : HTTP error code : "
11. }
12.
13. new BufferedReader(new
14. (conn.getInputStream())));
15.
16. String output;
17. "Output from Server .... \n");
18. while ((output = br.readLine()) != null) {
19.
20. System.out.println(output);
21. }
22.
23. conn.disconnect();
24.
25. catch
26.
27. e.printStackTrace();
28. catch
29.
30. e.printStackTrace();
31.
32. }
try {
URL url = new URL(
"http://localhost:8085/Resetjason/json/product/get");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
运行后,输出:
Output from Server ....
{"qty":999,"name":"iPad 3"}
5 调用服务端的POST,将JSON传入,看其如何转化位product
Java代码
1. try
2.
3. new
4. "http://localhost:8085/Resetjason/json/product/post");
5. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
6. true);
7. "POST");
8. "Content-Type", "application/json");
9.
10. "{\"qty\":100,\"name\":\"iPad 4\"}";
11.
12. OutputStream os = conn.getOutputStream();
13. os.write(input.getBytes());
14. os.flush();
15.
16. if
17. throw new RuntimeException("Failed : HTTP error code : "
18. + conn.getResponseCode());
19. }
20.
21. new BufferedReader(new
22. (conn.getInputStream())));
23.
24. String output;
25. "Output from Server .... \n");
26. while ((output = br.readLine()) != null) {
27.
28. System.out.println(output);
29. }
30.
31. conn.disconnect();
32.
33. catch
34.
35. e.printStackTrace();
36. catch
37.
38. e.printStackTrace();
39.
40. }
41.
42. }
try {
URL url = new URL(
"http://localhost:8085/Resetjason/json/product/post");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = "{\"qty\":100,\"name\":\"iPad 4\"}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
输出为:
Output from Server ....
Product created : Product [name=iPad 4, qty=100]
JAX-RS之jackson去处理json
原创
©著作权归作者所有:来自51CTO博客作者mb5c80f4c73b73a的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:JAX-RS之与XML打交道
下一篇:JAX-RS之上传文件
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
SpringBoot集成Jackson实现JSON序列化
SpringBoot集成Jackson实现JSON序列化
json Jackson SpringBoot -
集成JAX-RS和JSON-P
集成JAX-RS和JSON-P 一、JSON-P和JSON-B介绍在Java EE 7中引入了JSON Processing API,即JSON-P规范
json-p json-b javaee7 javaee8 json -
JAX-RS之@matrixParam和@PathParam
今天继续JBOSS RESETEASY学习之参数学习。今天要学习的分别是@PathParam 和@matr
string path url jboss date -
JAX-RS之@QueryParam和@DefaultValue
先来看@queryparam 先看例子: Java代码 Path("/users") public cla
path string java url date -
JAX-RS入门 一 :基础
简介JAX-RS是一套用java实现REST服务的规范,提供了一些标注将一个资源类,一
java 开发工具 jar Customer -
JAX-RS之resteasy跟spring整合
其实,在JAX-RS标准下,jboss的resteasy跟spring结合的话,无非是如何去取得 spring中的be
spring instantiation class string path -
JAX-RS规范-常用注解浅析
一、@Path
json java 服务器 客户端 xml -
JAX-RS入门教程
参考:http://liugang594.iteye.com/blog/1491434 springmvc rest
java spring mvc 实例代码 -
比较各JAX-RS实现
正如某人在别处说的,关于公交车,有一个奇怪的现象:你等了很久一辆不来,最后却一下来了三辆!JAX-RS实
spring xml sun公司 -
jax-rs注解工作原理介绍
基于 REST 的ieve, Update and Delete)操作与
java json 字节码 -
JAX-RS之下载文件
今天学习两个,分别是JAX-RS之下载文件 首先,看例子,下载服务器的文
file path object string 服务器