Understanding of Mavne Structure


Table of Contents
  1. @Controller
  2. @RestController
  3. @ResponseBody
  4. @RequestMapping and Derived Annotations
  5. @RequestParam
  6. @PathVariable
  7. @RequestBody
  8. @ResponseEntity

@Controller
@Controller
public class HomeController {
    @RequestMapping("/home")
    public String home() {
        return "home"; // returns view name (home.jsp or home.html from templates)
    }
}

@RestController
@RestController
@RequestMapping("/api")
public class UserController {
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World!"; // directly returns JSON response
    }
}

@ResponseBody
@Controller
public class UserController {
    @RequestMapping("/json")
    @ResponseBody
    public User getUser() {
        return new User("Kishor", "Bhosale"); // returned as JSON
    }
}

@RequestMapping and Derived Annotations
@RestController
@RequestMapping("/api/users")
public class UserController {

    @GetMapping("/{id}")
    public String getUser(@PathVariable int id) {
        return "User id: " + id;
    }

    @PostMapping
    public String createUser(@RequestBody User user) {
        return "Created user: " + user.getFirstName();
    }
}

@RequestParam
@RestController
@RequestMapping("/api")
public class UserController {

    @GetMapping("/fetchUser")
    public String getUserDetails(
        @RequestParam(required = false) String firstName,
        @RequestParam(required = false) String lastName) {
        return "User: " + firstName + " " + lastName;
    }
}
Custom Object Conversion using @InitBinder + PropertyEditor:
public class Address {
    private String city;
    private String state;
    // getters & setters
}

@RestController
@RequestMapping("/api")
public class AddressController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Address.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                String[] parts = text.split(",");
                Address addr = new Address();
                addr.setCity(parts[0]);
                addr.setState(parts[1]);
                setValue(addr);
            }
        });
    }

    @GetMapping("/address")
    public Address getAddress(@RequestParam Address address) {
        return address; // e.g. ?address=Pune,MH
    }
}

@PathVariable
@RestController
@RequestMapping("/api")
public class UserController {

    @GetMapping("/users/{id}")
    public String getUser(@PathVariable("id") int userId) {
        return "Fetched user id: " + userId;
    }
}

@RequestBody
public class User {
    private String firstName;
    private String lastName;
    // getters & setters
}

@RestController
@RequestMapping("/api")
public class UserController {

    @PostMapping("/create")
    public User createUser(@RequestBody User user) {
        return user; // JSON → User object
    }
}

@ResponseEntity
@RestController
@RequestMapping("/api")
public class UserController {

    @GetMapping("/status")
    public ResponseEntity<String> getStatus() {
        return ResponseEntity
                .status(HttpStatus.OK)
                .header("Custom-Header", "SpringBoot")
                .body("Everything is fine!");
    }
}