Home > Net >  How to test with Postman a controller's method that has one or more objects parameters (and not
How to test with Postman a controller's method that has one or more objects parameters (and not

Time:03-18

I am a newbie in Spring development. I need to create a simple application, a controller that has a method that takes as parameter an object of a custom designed entity class into the project. The prototype looks like this:

@RestController
public class JobsController {

@PostMapping("/search")
public ResponseEntity<?> search() {
    log.info("JobsController -> search method");
    //JobSearchEntity jobSearchEntity = modelMapper.map(jobSearch, JobSearchEntity.class);
    List<JobEntity> jobs = jobService.searchJobs();
    //log.info(String.format("Job found: %s ", jobSearch));
    return ResponseEntity.ok(jobs);
}
}

Can someone who is more advanced into this staff with Postman testing tell me how to do that , how to test a controller method which takes parameters?

CodePudding user response:

To get data from api is preferred to use GET method :

 @RestController
    public class JobsController {
    
    @GetMapping("/search")
    public ResponseEntity<?> search(@RequestParam("id") String id,@RequestParam("desc") String desc) {
        log.info("JobsController -> search method");
        //JobSearchEntity jobSearchEntity = modelMapper.map(jobSearch, JobSearchEntity.class);
        List<JobEntity> jobs = jobService.searchJobs();
        //log.info(String.format("Job found: %s ", jobSearch));
        return ResponseEntity.ok(jobs);
    }
    }

you call this api with post man this way :

post man request example

@PostMapping used usually to save new data (example : create job )

Take look on rest resource naming guide

CodePudding user response:

You can use postman to submit parameters in JSON format after adding @ requestbody annotation on the method, or submit parameters directly in form without annotation

CodePudding user response:

You can use this example. Is very simple exemple.

@RestController
@RequestMapping("/root")
public class RootController {
private final RootService service;

    public RootController(final RootService service) {
        this.service = service;
    }

  @PostMapping("/exemple")
    public void createRoot(@RequestBody final RootDto dto) {

        service.createRoot(dto);
    }
}

Then you can send request to POST host/root/exemple with your JSON.

More exampls you can find here: https://www.baeldung.com/spring-request-response-body

CodePudding user response:

It seems you are missing an honest search on google about the subject.

You can make use of @RequestBody annotation to accept method arguments. Check these page for examples -- @RequestBody and @ResponseBody annotations in Spring

https://stackabuse.com/get-http-post-body-in-spring/

https://www.twilio.com/blog/create-rest-apis-java-spring-boot

These set of playlist on youtube are very good starter course for SpringBoot - https://www.youtube.com/c/JavaBrainsChannel/playlists

Postman Tutorial-- https://www.youtube.com/watch?v=VywxIQ2ZXw4

  • Related