简体   繁体   中英

How do I test with Postman a Controller's method which has one (or multiple) objects as parameters in a Spring Application?

I an a novice with Spring framework. I have to create a simple application that searches for jobs in a database based on certain criteria, criteria which are sent to the controller's method via a parameter of an entity class specially designed for this scope. I attach the prototype of the method:

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class JobServiceImpl implements JobService {
    private final JobRepository jobRepository;
    private final JobTypeRepository jobTypeRepository;
    private final EntityManagerFactory entityManagerFactory;
    private final UserService userService;
    private final JobContactRepository jobContactRepository;

    @Override
    **public List<JobEntity> searchJobs(JobSearchEntity searchCriteria)** 
    { ...}

Does anyone know how does a method of this type (having parameters a special designed object) can be tested (called) in Postman? Is there any possibility to declare the "parameters of the parameters" of a method, such as is the case here...? Or is it possible to construct the object taken as parameter by the method in the Postman's graphical interface? Does anyone know how to do this task? Thanks in advance.

This isn't a controller, and so you can't talk to it with Postman. Find the controller where this service is used and inspect it there.

(Note that if you're using Spring, you'll often want to use Spring Data to simplify data access; manually using EntityManager is usually unnecessary, and you almost never want to use EntityManagerFactory directly.)

The JobServiceImpl you attached isn't a controller class. For Spring-MVC to recognize the class as a Controller you need to annotate the class with @Controller or @RestController annotation. Also, the method which will handle the URL mapping within the controller class needs to be annotated with @GetMapping or @PostMapping or @DeleteMapping, or so on.

@Contoller // or @RestController
public class JobServiceImpl {
    
    @GetMapping // @PostMapping or @DeleteMapping, etc
    public ResponseEntity mappingMethod() {
        // your code to handle request goes here
    }
}

yes but this is not the point, since the contreller method takes the same parameter with he same type, and all it does is call service;s method with the same parameter... this is the controller's method...

@ApiOperation(value = "Search job", notes = "With this request you can search job", authorizations = {@Authorization(value = "Bearer")})
@PostMapping("/search")
public ResponseEntity<?> search(JobSearchEntity searchCriteria) {
    log.info("JobsController -> search method");
    //JobSearchEntity jobSearchEntity = modelMapper.map(jobSearch, JobSearchEntity.class);
    List<JobEntity> jobs = jobService.searchJobs(searchCriteria);
    //log.info(String.format("Job found: %s ", jobSearch));
    return ResponseEntity.ok(jobs);
}

Anybody know the answer now...? thx.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM