How to add Rest Controller in Spring Boot?
In Spring Boot, you can add a controller by following these steps: Create a New Java Class: First, create a new Java class in your Spring Boot application. This class will serve as your controller. You can place it anywhere in your project's package structure, but it's a common practice to put it in a package named controller. Annotate the Class: Annotate the class with the @Controller annotation. This annotation tells Spring that this class is a controller and should be managed by the Spring container.
import org.springframework.stereotype.Controller;
@Controller
public class MyController {
// Controller code goes here
}
Define Request Mapping: Inside your controller class, define methods to handle specific HTTP requests. Use the @RequestMapping or more specific annotations like @GetMapping, @PostMapping, @PutMapping, or @DeleteMapping to map these methods to specific URLs.
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
In this example, the hello method is mapped to the /hello URL, and it returns "Hello, World!" when that URL is accessed via an HTTP GET request. Return Responses: Controller methods typically return a String representing a view name or use @ResponseBody to return data directly as JSON or other content types. In the example above, the method returns a String, which Spring Boot interprets as a view name, and Spring will attempt to find a matching view template. Configure View Resolver (if needed): If you are returning view names from your controller methods, you may need to configure a view resolver in your application.properties or application.yml file to map view names to actual view templates. For example, if you're using Thymeleaf as your template engine, you can configure it like this:
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
Run Your Application: Start your Spring Boot application. Spring Boot will automatically scan for your controller classes and set up the necessary mappings. Access Your Controller: Open a web browser or use a tool like curl or Postman to access the URLs mapped to your controller methods. In the example above, you can access the /hello URL to see the "Hello, World!" response.
Spring Boot Startup example:-
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class MainClassForSpringBootApp {
public static void main(String[] args) {
SpringApplication.run(MainClassForSpringBootApp.class, args);
}
// creating an endpoint
@GetMapping("/Hi")
public String GreetingMsg() {
return "Welcome to techieuncle.com";
}
}
Now run the main class and watch till the console as Tomcat started on port(s): 8080 (http) with context path ''
Now open chrome and test endpoint as http://localhost:8080/Hi
output will be Welcome to techieuncle.com
RestController provides rest api.