JSON example with Jersey + Jackson
来源:互联网 发布:qt linux sleep头文件 编辑:程序博客网 时间:2024/04/26 21:00
Jersey uses Jackson to convert object to / form JSON. In this tutorial, we show you how to convert a “Track” object into JSON format, and return it back to user.
1. Dependency
To make Jersey support JSON mapping, declares “jersey-json.jar
” in Maven pom.xml
file.
<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.8</version> </dependency>
2. Integrate JSON with Jersey
In web.xml
, declares “com.sun.jersey.api.json.POJOMappingFeature
” as “init-param
” in Jersey mapped servlet. It will make Jersey support JSON/object mapping.
<init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param>
File : web.xml
– full example.
<web-app ...> <servlet> <servlet-name>jersey-serlvet</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.mkyong.rest</param-value> </init-param> <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping></web-app>
3. Simple Object
A simple “Track” object, later Jersey will convert it into JSON format.
package com.mkyong;public class Track { String title; String singer; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSinger() { return singer; } public void setSinger(String singer) { this.singer = singer; } @Override public String toString() { return "Track [title=" + title + ", singer=" + singer + "]"; }}
4. JAX-RS with Jersey
Annotate the method with @Produces(MediaType.APPLICATION_JSON)
. Jersey will use Jackson to handle the JSON conversion automatically.
package com.mkyong.rest;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.MediaType;import javax.ws.rs.core.Response;import com.mkyong.Track;@Path("/json/metallica")public class JSONService { @GET @Path("/get") @Produces(MediaType.APPLICATION_JSON) public Track getTrackInJSON() { Track track = new Track(); track.setTitle("Enter Sandman"); track.setSinger("Metallica"); return track; } @POST @Path("/post") @Consumes(MediaType.APPLICATION_JSON) public Response createTrackInJSON(Track track) { String result = "Track saved : " + track; return Response.status(201).entity(result).build(); }}
5. Demo
See demo for GET and POST request.
1. GET method
When URI pattern “/json/metallica/get
” is requested, the Metallica classic song “Enter Sandman” will be returned in JSON format.
{ "singer":"Metallica", "title":"Enter Sandman"}
2. POST method
To test post request, you can create a RESTful client (refer to this Jersey client APIs example), and “post” the json format string to URI pattern “/json/metallica/post
“, the posted json string will be converted into “Track” object automatically.
- JSON Example With Jersey + Jackson
- JSON example with Jersey + Jackson
- JSON Example With RESTEasy + Jackson
- JSON Example With RESTEasy + Jackson
- JSON example with RESTEasy + Jackson
- Jersey 配合 Jackson 输出 json
- XML example with Jersey + JAXB
- RESTful Web Services Example in Java with Jersey, Spring
- jersey spring jackson
- jQuery Ajax Example With JSON Response
- JSON example with RESTEasy + JAXB + Jettison
- Jersey hello world example
- JAX-RS (Jersey) custom exception with XML or JSON
- Jackson Tree Model Example
- Jackson JSON
- json-> jackson
- File upload example in Jersey
- json example
- OGG安装配置文档
- Android studio中正确引入so文件的方法
- python文件中的__name__=='__main__'的使用及调用其他py文件中的函数方法
- uva 10084 - Hotter Colder(多边形切割)
- Tigase开发笔记6:packet流转机制 -> 一条消息(packet)的请求和响应过程解析
- JSON example with Jersey + Jackson
- 架构师好比两面神
- Apache与Tomcat有什么关系和区别
- 数据结构学习之路-第二章:静态单链表
- 桥设备透传802.1x报文
- uva 12307 - Smallest Enclosing Rectangle(旋转卡壳)
- Unity全景项目经验Q&A
- Multiset(中)——STL中的multiset
- html5 canvas 详细使用教程