Controllers In Spring Framework For Java Beginners
It is really hard to build web application if you are just a beginner, but if you spare your time seriously learning web development, you're eventually gonna make it. Spring is a Java framework for web development, by using framework, you don't to focus on some core stuff like http stuff, security, database connection, all you gonna need to do is just some little configuration and more focus on writing business logic for your application.
If you've already finish the first part and successfully running your application on the browser, the next step is you just need to learn more, to read more stuff about spring framework. This part will be just about controller. Controller is to where you decide an endpoint, usually on web application, user will request a page or a service, we called it end point.
1. Simple hello world controller
@Controller
@RequestMapping("hello")
public class HelloController{
@GetMapping
public String hello(){
return "Hello World!";
}
}
- @Controller is annotation to make this class as controller
- @RequestMapping is annotaion where you spesifing your end point uri
- @GetMapping is to spesifing that it will accept only GET request
So we now we have an end point /hello, will just return hello word as string.
2. Get the request id
It's happen a lot, you need to get something on your application by using an id, the endpoint is usually for example /user/123, we want to get user with id 123. to get the id in spring, we can use PathVariable annotation.
@Controller
@RequestMapping("user")
public class UserController{
@GetMapping("/{id}")
public String hello(@PathVariable Long id){
return "The requested id is "+id;
}
}
3. Post data
Post data usually triggered when someone hit a send button of form on the front end application, in spring to handle a post method we can use @PostMapping annotation, and we create a class representing the data we're gonna get, so we have to create at least two classes.
@Controller
@RequestMapping("user")
public class UserController{
@PostMapping("/signup")
public Registation hello(@RequestBody Registration registration){
return registration;
}
}
class Registration{
private String name;
private String email;private String password;
}
The class registration is representation of registration form, it usually has name, username, email, password. If you need more add some more fields into registration class. The end point will return the data that user submitted.
4. Delete and Put mapping
Usually in a rest application, it always has delete and put, delete is for deleting your data, and put for updating the data.
@Controller
@RequestMapping("user")
public class UserController{
@DeleteMapping("/{id}")
public String hello(@PathVariable Long id){
return "user id to delete is "+id;
}
@PutMapping("/{id}")
public Registation hello(@PathVariable Registration registration){
return "user id to delete is "+id;
}
}
5. Parameter
In a URL, we seen a lot of parameter, for example /user?limit=10, it basically a request to find all user limited by 10, so to get the 10 from the parameter we use RequestParam annotation
@Controller
@RequestMapping("user")
public class UserController{
@GetMapping
public String hello(@RequestParam Long limit){
return "limit is "+limit;
}
}