Searching...
Wednesday, 14 August 2013

Spring Restful Web Services + JAXB Example

11:11
Spring frameworks provides the Restful web services implementation, here is one simple example to implement Web services using Spring and uses JAXB for response binding.

Softwares Used

  • Java 1.8
  • Spring 4
  • Jetty Server


Create a maven project using web-app archetype and add below dependencies

<dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.2.3.v20140905</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>9.2.3.v20140905</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        

Create a Controller class to define our service
 
package com.vinod.vinod_spring_test;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mystate")
public class JaxbController {
    @RequestMapping(value = "/{code}", method = RequestMethod.GET)
    public State getState(@PathVariable String code) {
        String result;
        if (code.equals("KL")) {
            result = "Kerala";
        } else {
            result = "Default State";
        }
        State st = new State();
        st.setCode(code);
        st.setName(result);
        return st;

    }
}

State.java

package com.vinod.vinod_spring_test;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessType;

@XmlRootElement(name = "state")
@XmlAccessorType(XmlAccessType.NONE)
public class State {
    @XmlElement(name = "name")
    private String name;
    @XmlElement(name = "code")
    private String code;

    //Getters and setters
}

Spring configuration class

package com.vinod.vinod_spring_test;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = {
        "com.vinod.vinod_spring_test"})
public class MyWebServiceSpringConfig{

}

Main class to start Jetty server

package com.vinod.vinod_spring_test;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class JettyWebserviceStart {

    public static void main(String[] args) {
        final AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.register(MyWebServiceSpringConfig.class);
        final ServletHolder servletHolder = new ServletHolder(new DispatcherServlet(applicationContext));
        final ServletContextHandler context = new ServletContextHandler();
        context.setContextPath("/");
        context.addServlet(servletHolder, "/*");
        final Server server = new Server(8080);
        server.setHandler(context);
        try {
            server.start();
            server.join();
        } catch (Exception e) {
            server.destroy();
            e.printStackTrace();
        }
    }

}
  

Output

Run the above program and hit the below url

http://localhost:8080/mystate/KL




Download examples

https://github.com/kkvinodkumaran/spring

0 comments: