Loading
 
What's Next: View All Tutorial →

Spring Boot Microservice using @RestController and produces JSON and XML response

A simple program to create a spring boot microservice    @RestController is a specialized version of the controller. It includes the @Controller and @ResponseBody ...

Published Date: 05/09/2018  Last Modified Date: 05/09/2018    New Post



A simple program to create a spring boot microservice 
 

@RestController is a specialized version of the controller. It includes the @Controller and @ResponseBody annotations and as a result, simplifies the controller implementation
 

Maven Dependency

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demo</groupId>
    <artifactId>spring-boot-restcontroller</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <name>spring-boot-restcontroller</name>
    <description>Demo project for Spring Boot Web MVC</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
 

Application.java

package com.demo.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}
 

MyRestController.java

package com.demo.spring;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyRestController {

	@RequestMapping(path = "/greet/{name}")
	public String welcome(@PathVariable("name") String name, HttpServletRequest request) {
		String city = request.getParameter("city");
		return "Welcome " + name + city;
	}

	@RequestMapping(path = "/getJson", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Employee getJson() {
		Employee emp = new Employee();
		emp.setCity("chennai");
		emp.setName("John");
		return emp;
	}

	@RequestMapping(path = "/getXML", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
	public @ResponseBody Employee getEmployeeInXML() {
		Employee emp = new Employee();
		emp.setCity("chennai");
		emp.setName("John");
		return emp;
	}

	@PostMapping(path = "/saveEmployee", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
	public @ResponseBody Employee getEmployeeInXML(@RequestBody Employee emp) {
		return emp;
	}
}
 
 
produces = MediaType.APPLICATION_XML_VALUE  - Return XML response (Internal JAXB jar available
 
produces = MediaType.APPLICATION_JSON_VALUE  - Return json response

@RestController- @RestController is a specialized version of the controller. It includes the @Controller and @ResponseBody annotations and as a result, simplifies the controller implementation

@PostMapping - Alternate way of writing @RequestMapping, method=RequestMethod.POST,   

@RequestBody accept based on the cosumes type

The controller is annotated with the @RestController annotation, therefore the @ResponseBody isn’t required.
 

Employee.java - Bean to produce json or XML

 
package com.demo.spring;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Employee")
public class Employee {

    private String name;

    private String city;

    public String getName() {
        return name;
    }

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

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

}
 

Output:

 

Rest Url #1

http://localhost:8080/greet/John?city=chennai

Welcome Johnchennai

Rest Url #2

http://localhost:8080/getJson

{"name":"John","city":"chennai"}

 

Rest Url #3

http://localhost:8080/getXML
 

<Employee>
<city>chennai</city>
<name>John</name>
</Employee>



Sample code

https://github.com/Catchmycity/Springboot/tree/master/spring-boot-restcontroller

 
Awaiting for Administrator approval




← Back to list


Related Post




×