Total Pageviews

2018/10/12

[Spring Boot] How to create a simple restful service

Scenario



How-To
Restful Controller:
package springboot.rest.controller;

import java.util.Date;

import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import springboot.rest.vo.Greeting;

@RestController
@RequestMapping("/api/greeting")
public class GreetingController {

    @GetMapping("/get/sayHi")
    public String greeting(@RequestParam(value = "name") String name) throws JsonProcessingException {
        Greeting greeting = new Greeting("Hello, " + name, new Date());
        String greetingJson = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            greetingJson = objectMapper.writeValueAsString(greeting);
        } catch (JsonProcessingException e) {
            throw e;
        }
        return greetingJson;
    }

    @PostMapping(value = "/post/sayHi")
    public String greeting(@RequestBody Greeting greeting) throws JsonProcessingException {
        Greeting greetingVo = new Greeting(greeting.getContent(), new Date());
        String greetingJson = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            greetingJson = objectMapper.writeValueAsString(greetingVo);
        } catch (JsonProcessingException e) {
            throw e;
        }
        return greetingJson;
    }

}


Value Object:
package springboot.rest.vo;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class Greeting {

    private String content;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss.SSS", timezone = "GMT+8")
    private Date datetime;
}


Make the application executable:

package springboot.rest;

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

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


pom.xml
<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>springboot</groupId>
    <artifactId>rest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>rest</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <jackson.version>2.9.5</jackson.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
    </dependencies>
</project>


Test Result:







No comments: