I. Environment Introduction (1) Jersey2.5.1 (2) Eclipse3.7Tomcat7JDK7 II: build progress (1) download jaxrs-ri-2.5.1.zip through the address jersey.java.net. (2) create a dynamic project RestDemo through Eclipse. (3)decompress jaxrs-ri-2.5.1.zip, copy the JAR package in the list to RestDemoW
I. Environment Introduction (1) Jersey2.5.1 (2) Eclipse 3.7 Tomcat 7 JDK 7 2: Build Steps (1) download jaxrs-ri-2.5.1.zipat https://jersey.java.net. (2) create a dynamic project RestDemo through Eclipse. (3)decompress jaxrs-ri-2.5.1.zip, copy the JAR package in the list to RestDemo \ W
I. Environment Introduction
(1) Jersey2.5.1
(2) Eclipse 3.7 + Tomcat 7 + JDK 7
Ii. Setup steps
(1) download jaxrs-ri-2.5.1.zip Via address https://jersey.java.net.
(2) create a dynamic project RestDemo through Eclipse.
(3)decompress jaxrs-ri-2.5.1.zip, copy the JAR package in the list to RestDemo \ WebContent \ WEB-INF \ lib.
(4) Create the package path cn.com. vs. vo and create User. java:
Package cn.com. vs. vo; Import javax. xml. bind. annotation. XmlRootElement; @ XmlRootElement Public class User { Private String name; Private String age; Private String sex; Public String getName (){ Return name; } Public void setName (String name ){ This. name = name; } Public String getAge (){ Return age; } Public void setAge (String age ){ This. age = age; } Public String getSex (){ Return sex; } Public void setSex (String sex ){ This. sex = sex; } } |
(5) Create the package path cn.com. vs. service and create RestService. java:
Package cn.com. vs. service; Import javax. ws. rs. GET; Import javax. ws. rs. Path; Import javax. ws. rs. Produces; Import javax. ws. rs. core. MediaType; Import cn.com. vs. vo. User; @ Path ("/restService ") Public class RestService { @ GET @ Path ("/getUserText ") @ Produces (MediaType. TEXT_PLAIN) Public String getUserText (){ Return "Hello, World! "; } @ GET @ Path ("/getUserXml ") @ Produces (MediaType. APPLICATION_XML) Public User getUserXml (){ User user = new User (); User. setName ("snail Il "); User. setAge ("22 "); User. setSex ("male "); Return user; } @ GET @ Path ("/getUserJson ") @ Produces (MediaType. APPLICATION_JSON) Public User getUserJson (){ User user = new User (); User. setName ("snail Il "); User. setAge ("22 "); User. setSex ("male "); Return user; } } |
(6) Create RestApplication. java class under the cn.com. vs package path
Package cn.com.; Import org. glassfish. jersey. filter. LoggingFilter; Import org. glassfish. jersey. server. ResourceConfig; Public class RestApplication extends ResourceConfig { Public RestApplication (){ // Path of the service package Packages ("cn.com. vs. service "); // Print the access log to facilitate tracking and debugging. The log can be cleared after the official release. Register (LoggingFilter. class ); } } |
(7) modify web. xml with the following content:
RestDemo Mobile Org. glassfish. jersey. servlet. ServletContainer Javax. ws. rs. Application Cn.com. vs. RestApplication 1 Mobile /Rest /* Index.html |
(8) Deploy and run it in Tomcat7 to view the release results:
Get text data:
Get XML data:
To obtain JSON data: http: // localhost: 8080/RestDemo/rest/restService/getUserJson, the following exception occurs:
[Org. glassfish. jersey. message. internal. messageBodyProviderNotFoundException: MessageBodyWriter not found for media type = application/json, type = class cn.com. vs. vo. user, genericType = class cn.com. vs. vo. user.] with root cause Org. glassfish. jersey. message. internal. messageBodyProviderNotFoundException: MessageBodyWriter not found for media type = application/json, type = class cn.com. vs. vo. user, genericType = class cn.com. vs. vo. user. |
(If you run through the glassfish-4.0, there is no problem! If tomcat is used, you need to add a JSON converter.
(9) solve the JSON Data Acquisition exception. Method 1:
A) download the jackson-all-1.9.11.jar and put it under lib.
B) modify RestApplication. java with the following content:
Package cn.com.; Import org. codehaus. jackson. jaxrs. JacksonJsonProvider; Import org. glassfish. jersey. filter. LoggingFilter; Import org. glassfish. jersey. server. ResourceConfig; Public class RestApplication extends ResourceConfig { Public RestApplication (){ // Path of the service package Packages ("cn.com. vs. service "); // Register the JSON Converter Register (JacksonJsonProvider. class ); // Print the access log to facilitate tracking and debugging. The log can be cleared after the official release. Register (LoggingFilter. class ); } } |
(10) solve the JSON Data Acquisition exception. Method 2:
A) download the glassfish-4.0.zip, decompress, copy the following JAR to lib:
B) modify RestApplication. java with the following content:
Package cn.com.; Import org. glassfish. jersey. filter. LoggingFilter; Import org. glassfish. jersey. jackson. JacksonFeature; Import org. glassfish. jersey. server. ResourceConfig; Public class RestApplication extends ResourceConfig { Public RestApplication (){ // Path of the service package Packages ("cn.com. vs. service "); // Register the JSON Converter Register (JacksonFeature. class ); // Print the access log to facilitate tracking and debugging. The log can be cleared after the official release. Register (LoggingFilter. class ); } } |
(11) select one of the above methods, and then publish and start to obtain JSON data. The result is as follows:
The attachment is my demo, because the latest version is jersey2.13. I am using jersey2. 13.