:::: MENU ::::

Spring Boot


Spring Boot is a tool that generates a Maven project with Spring and other specified technologies. In this case we’ll use Jersy, that is used to create Web Rest services.

Use of Spring Boot Inizializr

Go to the Spring Boot Inizializr page (http://start.spring.io/) and in Search for depencencies search and select “Jersey (JAX-RS)”. After that, click on “Generate Project” and a ZIP will be automatically downloaded with the project base.

In Eclipse, import this project as Maven Project and wait until all the dependencies are downloaded.

Java beans

First we have to do is to create the Java beans that will content the JSON structure to be delivered by our Web service. We create a MyObject class and other that will be a list of MyObjects.

MyObject

package com.luisgomezcaballero.springbootjerseydemo;

import java.io.Serializable;

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

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "myObject")
public class MyObject implements Serializable {

	private static final long serialVersionUID = 1L;

	@XmlAttribute(name = "myInt")
	private int myInt;

	@XmlAttribute(name = "myString")
	private String myString;

	public MyObject(int myInt, String myString) {
		super();
		this.myInt = myInt;
		this.myString = myString;
	}

	public int getMyInt() {
		return myInt;
	}

	public void setMyInt(int myInt) {
		this.myInt = myInt;
	}

	public String getMyString() {
		return myString;
	}

	public void setMyString(String myString) {
		this.myString = myString;
	}

}

MyObjectList

package com.luisgomezcaballero.springbootjerseydemo;

import java.util.ArrayList;

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

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "myObjectList")
public class MyObjectList {

	@XmlElement(name = "myObject")
	private ArrayList<MyObject> myObjectList;

	public ArrayList<MyObject> getMyObjectList() {
		return myObjectList;
	}

	public void setMyObjectList(ArrayList<MyObject> myObjectList) {
		this.myObjectList = myObjectList;
	}

}

Controller

With these two componentes, we just need to create a controller class that will use them and will deliver them as JSON format.

MyController

package com.luisgomezcaballero.springbootjerseydemo;

import java.util.ArrayList;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "objects")
@Path("/objects")
public class MyController {

	@GET
	@Produces("application/json")
	public MyObjectList getAllObjects() {

		ArrayList<MyObject> myList = new ArrayList<>();
		myList.add(new MyObject(1, "abc"));
		myList.add(new MyObject(2, "def"));
		myList.add(new MyObject(3, "ghi"));

		MyObjectList myObjectList = new MyObjectList();
		myObjectList.setMyObjectList(myList);

		return myObjectList;
	}
}

Project configuration

Finally it is needed to configure the project to register the componentes.

JerseyConfig

package com.luisgomezcaballero.springbootjerseydemo;

import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;

@Component
public class JerseyConfig extends ResourceConfig {

	public JerseyConfig() {
		register(MyController.class);
	}
}

SpringBootJerseyDemoApplication

package com.luisgomezcaballero.springbootjerseydemo;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootJerseyDemoApplication extends SpringBootServletInitializer {

	public static void main(String[] args) {
		new SpringBootJerseyDemoApplication()
				.configure(new SpringApplicationBuilder(SpringBootJerseyDemoApplication.class)).run(args);
	}
}

Result

We launch the applictaion with “Run as…/Java Application” on the class that contains the main() method, which in this case is  SpringBootJerseyDemoApplication.java.

When accessing to the URI http://localhost:8080/objects we will see how it delivers a JSON object.

Repository

The code of this project is located at https://github.com/luisgomezcaballero/spring-boot-jersey-demo.


So, what do you think ?