Skip to main content

RESTful API Hello World with Spring Boot

Reference

  1. Spring Microservices in Action, Manning, 2017 Jun, https://www.manning.com/books/spring-microservices-in-action
  2. Getting Started · Building a RESTful Web Service, https://spring.io/guides/gs/rest-service/

Environment

  • Maven: 3.5.2
  • JDK: 1.8.0_131, vendor: Oracle Corporation
  • OS: "windows 10"
  • IDE Spring Tool Suite 3.9.4.RELEASE


1. Create the Maven project



https://start.spring.io/

  • Maven project with Java and Spring Boot: 1.5.13 (failure to start) 1.5.10
  • Group: com.wonderland.api
  • Artifact: helloworld-service
  • Dependencies:
    • Actuator #for the metric later
    • Web
    • Rest Repositories
    • Eureka Discovery #for the API registration and discover later

Download: helloworld-api.zip
Unzip into {project folder}
Import into IDE: File > Import > Existing Maven Projects > {project folder} > Finish

2. Change the Code

2.1. Add class Greeting with attribute name #force the JSON response with Object return

2.2. Add class HelloworldController with @RestController
2.2.1. Add method:
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name)

2.3. Update class HelloworldApiApplication with @EnableEurekaClient


3. Configure the Spring Boot application

Edit application.yml (rename the application.properties into application.yml)

spring: 
    application:
        name: helloworld-api

server:
    port: 10010

logging:
    level:
        com.wonderland.api.helloworldapi: DEBUG

management:
    security:
        enabled: false

eureka:     
    instance:
        leaseRenewalIntervalInSeconds: 5
        leaseExpirationDurationInSeconds: 2
    client:
        serviceUrl:
            defaultZone: http://localhost:30100/eureka/


4. Test the API

#> {project folder}\mvn spring-boot:run
http://localhost:10010/hello
http://localhost:10010/hello?name=julian

4.1. Expected error about Eureka registration #to be resolved



Comments

Popular posts from this blog

人間好時節

春有百花秋有月 夏有涼風冬有雪 若無閒事掛心頭 便是人間好時節 本星期十常八 九之事比較多, 心情較為低沉. 此時最住解決方案就是做好本份, 好好學習同常想一二.

Novel

 全球異能:開局覺醒紫霄神雷章節列表 - 稷下書院 https://www.novel543.com/1215500675/ 御獸時代,我開局神級天賦 https://www.novel543.com/1001497097/ 混沌天帝决 https://t.shuqi.com/book/7978589.html