Building Restful API using Springboot

1.Create Spring-boot project

IntelliJ IDEA CE doesn’t support “spring initializr” plugin, so we should initial a SpringBoot project using https://start.spring.io/

Download and unzip project, import it into IntelliJ.

2. Coding
  • Using  @RestController annotation which equals :  @controller + @ResponseBody
  •  Springboot main class should be in the same package with controllers and it should be in the root.

  • Code Controller:

@RestController
public class UserController {

    @RequestMapping(value = "/users/{uuid}", method = RequestMethod.GET)
    public ResponseEntity getUser(@PathVariable("uuid") String uuid) {
        UserJson userJson = new UserJson(uuid, "bob");
        return new ResponseEntity<>(userJson, HttpStatus.OK);
    }
}
  • Code Entity:

public class UserJson {

    @JsonProperty(value = "uuid")
    private String uuid;

    @JsonProperty(value = "name")
    private String name;

    public UserJson(String uuid, String name) {
        this.uuid = uuid;
        this.name = name;
    }
}
  • Run project

Configration Context and Port (Modifiy application.properties)


./gradlew build bootRun

Source Code Github

Author: aerodc

Software Engineer

Leave a Reply

Your email address will not be published. Required fields are marked *