Rest API Example in java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.test;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Admin
 */
@XmlRootElement(name = "registration")
public class StudentRegistration {

    String name;
    int roolnumber;
    String location;

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement
    public String getLocation() {
        return location;
    }

    public int getRoolnumber() {
        return roolnumber;
    }

    @XmlAttribute
    public void setRoolnumber(int roolnumber) {
        this.roolnumber = roolnumber;
    }

    public void setLocation(String location) {
        this.location = location;
    }

}

====================
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.tutorialspoint.StudentRegistration;

/**
 *
 * @author Admin
 */

@Path("/students")
public class StudentsREST {

    @GET
    @Path("/{roolnumber}/{name}/{location}")
    @Produces(MediaType.APPLICATION_XML)
    public StudentRegistration getStudents(@PathParam("roolnumber") int roolnumber,@PathParam("name")String name, @PathParam("location")String location) {

        StudentRegistration register = new StudentRegistration();

        register.setName(name);
        register.setLocation(location);
        register.setRoolnumber(roolnumber);


        return register;

    }

}

===============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_3_0.xsd"
   id="WebApp_ID" version="3.0">
   <display-name>User Management</display-name>
   <servlet>
      <servlet-name>Jersey RESTful Application</servlet-name>
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
         <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.test</param-value>
         </init-param>
      </servlet>
   <servlet-mapping>
   <servlet-name>Jersey RESTful Application</servlet-name>
      <url-pattern>/rest/*</url-pattern>
   </servlet-mapping> 
</web-app>

PS: Add all the required Jars




Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Java Design pattern

Map class field to map