Jersey Building Rest Service returns JSON data

Source: Internet
Author: User

1. Eclipse creates a dynamic Web project

2. Place the jersey-related jar package in the Libs directory

3. Web. XML adds jersey related content

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 "> <display-name>restdemo</ display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file& Gt;index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file> Default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file> default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>jersey REST Servic E</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.huawei.rest.resources</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> </ Web-app>

4. Create a resource class

Package Com.huawei.rest.resources;import Javax.ws.rs.GET;  Import Javax.ws.rs.Path;  Import javax.ws.rs.Produces;  Import Javax.ws.rs.PathParam;  Import Javax.ws.rs.core.MediaType;    @Path ("/hello") public  class Helloresource {      @GET      @Produces (mediatype.text_plain) public      String SayHello () {          return "Hello Jersey, first demo";      }               @GET      @Path ("/{param}")        @Produces ("Text/plain;charset=utf-8") public      String sayHelloToUTF8 (@ Pathparam ("param") String username) {          return "Hello" + username;      }          @GET      @Path ("/getuser")      @Produces (mediatype.application_json) public      User Getuserjson () {       user User  = new user ();       User.setage (+);       User.setuserid ("005");       User.setusername ("Fmand");       return user;      }      }  

5. Create the User class

Package Com.huawei.rest.resources;import javax.xml.bind.annotation.XmlRootElement; @XmlRootElementpublic class User {int age; String userid; String username;public int Getage () {return age;} public void Setage (int.) {this.age = age;} Public String GetUserid () {return userid;} public void Setuserid (String userid) {this.userid = userid;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;}}

6. Run a look at the effect

Source code: http://download.csdn.net/detail/mtour/9592162

Jersey Building Rest Service returns JSON data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.