2 June 2019
Spring boot controller
by Jerry Zhang
Template
If you only use @Controller instead of @RestController, then you have to use a template like Thymeleaf.
- In pom,xml, import thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
-
Create a html file in template folder, for example, say “Hello World” in a <h1> tag.
-
In controller java class, @Controller and return the name of that html file.
@Controller
public class HelloController {
@Autowired
private LimitConfig limitConfig;
@GetMapping("/hello")
public String say(){
// return "Description: " + limitConfig.getDescription();
return "index";
}
}
@Controller + @ResponseBody = @RestController
If you want to return both html and String/Json data, then you can @Controller on the class level, and @ResponseBody on the method level. However, this approach is not recommended. Nowadays, we usually separate back-end and front-end.
url
Multiply url for a same method
@GetMapping({"/hello", "/hi"})
public String say(){
return "Description: " + limitConfig.getDescription();
}
url chain
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private LimitConfig limitConfig;
@GetMapping("/say")
public String say(){
return "Description: " + limitConfig.getDescription();
}
}
POST and GET
It is better to Specify the request method, either GetMapping or PostMapping. It is not recommended to use RequestMapping both a method, which means both will work.
Request Parameters
- @PathVariable
@GetMapping("/say/{id}")
public String say(@PathVariable("id") Integer id){
return "id: " + id;
}
- @RequestParam
@GetMapping("/say")
public String say(@RequestParam("id") Integer id){
return "id: " + id;
}
- Optional parameters
@GetMapping("/say")
public String say(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id){
return "id: " + id;
}
- postman
To send a Post request with parameters, use postman, and choose Body
, then choose x-www-form-urlencoded