Zum Inhalt springen

Apihit using postman

I have created two classes: one is a Controller, and the other is a Service.

The Controller class is annotated with @RestController, and the Service class is annotated with @Service. These annotations are recognized by Spring to identify which class serves as a Controller and which one acts as a Service.

Both @Controller and @Service are meta-annotated with @Component, which means Spring will automatically detect and create objects (beans) for these classes during component scanning.

In the Controller class, I used the @Autowired annotation to inject the Service class, allowing the Controller to call the service layer methods.

In the Controller, I am using the @RestController annotation. This is a convenience annotation that combines @Controller and @ResponseBody. It indicates that the return value of the methods should be bound to the web response body, making it suitable for REST APIs.

In Spring Boot, the @PostMapping annotation maps HTTP POST requests to a specific handler method in the Controller. It’s a shortcut for @RequestMapping(method = RequestMethod.POST) and is commonly used to create new resources on the server.
@PostMapping is often used with the @RequestBody annotation. The @RequestBody annotation binds the HTTP request body to a method parameter, allowing the server to receive structured data (such as JSON or XML) and convert it to a Java object

When using Postman, we send input in JSON format. However, Java doesn’t have a direct „JSON“ type. To handle this, I created a Student POJO class with appropriate getter and setter methods. The @RequestBody annotation is used to automatically map the incoming JSON data to the Java Student object.

@RestController
public class StudentController {

@Autowired
private StudentService studentService;

@PostMapping("/studentDetails")
public Student studentDetails(@RequestBody Student student) {
    studentControllerlog.debug("Inside student controller");
    return studentService.processStudent(student);
}

}

Image description

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert