# Re:0 - Starting Life in Backend

seal

我主张制止不了就放任
这欲望与绝望之争
余温她却喜欢过门
临走呢
还随手关了灯


Starting today,I'll begin learning Spring Boot,in order to fit some of my cooperation with ZJU's needs.

To use Spring Boot, I choose Intellij IDEA as IDE, and use Maven to build.


Level 1: Getting Started with Spring Boot - Building Your First Web Application

Target: Create an easy Spring Boot Web app, exposes a RESTful API, returns "Hello,Spring Boot!"

here is a solution

package com.example.demo2;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController        //@RestController annotation combines @Controller and @ResponseBody
//Indicating that this class is a RESTful API controller, and the data returned by this method will be directly used as the HTTP response body
public class HelloWorldController {

    @GetMapping("/hello")         //@GetMapping annotation maps HTTP GET request to specific processing methods
    //here maped "/hello" route to implements
    public String WriteHelloWorld() {
        return "Hello,Spring Boot!";
    }

}

test this code ,using http://127.0.0.1:8081/hello in postman,returns Hello,Spring Boot!
Win

Level 1-s

In the above example, I use the 8081 port, but how to change it?🙀

We need to change application.properties in the src/main/resources directory, add server.port=8081 in the file.

Level 2: Data Interaction - GET Request and Path Parameters

Target: Create a RESTful API ,enable to receive datas ,and returns a string with the parameter value

solution:

@GetMapping("/greet/{name}")             //here difines a path parameter "name"
public String Greet(@PathVariable String name ){        //@PathVariable annotation is used to bind variables in the URL path to method parameters
    return "Hello " + name + "!";
}

Level 3: Data Submission - POST Request and Request Body

Target: Create a POST interface that receives a JSON request body and processes the incoming data

solution:

//model
package com.example.demo2.model;

import com.fasterxml.jackson.annotation.JsonProperty;

public class User {

    @JsonProperty("username")
    private String username;
    @JsonProperty("age")
    private int age;

    public User(){

    }

    public User(String name, int age) {
        this.username = name;
        this.age = age;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return username + " " + age;
    }
}
//controller
package com.example.demo2.controller;

import com.example.demo2.model.User;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @PostMapping("/user")
    public String User(@RequestBody User user) {
        return user.toString();
    }
}

here we create a new package——model , this package is used to support complex data structure reading.

Level 3-s

In the above example, we use the @RequestBody annotation, this annotation can read completes objects and complex data structures ,but they need to be deserialized(反序列化)

In fact , @RequestBody is same at @RequestParams in a sense.
The above reading steps can also be written like this:

public String User(
    @RequestParam(name = "username" , required = false , defaultValue = "null") String username,
    @RequestParam(name = "age" , required = false , defaultValue = 0) int age
) {
    User user = new user(username, age);
    return user.toString();
}

However,it needs to be appended as a query parameter to the URL of the POST request;This code no longer processes JSON request bodies, because @RequestParam does not parse JSON.

So,using @RequestBody in POST method is extremely a better way.