Jersey builds rest services and returns json data. jersey builds restjson

Source: Internet
Author: User

Jersey builds rest services and returns json data. jersey builds restjson

1. Create a dynamic web project in eclipse

2. Place the jersey jar package in the libs directory.

3. Added jersey content in web. xml.

<?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>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 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.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(27);       user.setUserid("005");       user.setUsername("Fmand");       return user;      }      }  

5. Create a 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 age) {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 the command to check the effect.

 

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

 

Related Article

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.