简体   繁体   English

如何使用 Postman 测试控制器的方法,该方法在 Spring 应用程序中将一个(或多个)对象作为参数?

[英]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.我是 Spring 框架的新手。 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:我必须创建一个简单的应用程序,根据特定标准在数据库中搜索工作,这些标准通过专为此 scope 设计的实体 class 的参数发送到控制器的方法。我附上该方法的原型:

@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?有谁知道如何在 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?或者是否可以通过Postman图形界面中的方法构造object作为参数? 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.这不是 controller,因此您不能与 Postman 通话。找到使用此服务的 controller 并在那里进行检查。

(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.) (请注意,如果您正在使用 Spring,您通常会希望使用 Spring 数据来简化数据访问;手动使用EntityManager通常是不必要的,而且您几乎不想直接使用EntityManagerFactory 。)

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.您附加的 JobServiceImpl 不是 controller class。要使 Spring-MVC 将 class 识别为 Controller,您需要使用@Controller@RestController注释对 class 进行注释。 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.此外,将处理 controller class 中的 URL 映射的方法需要使用 @GetMapping 或 @PostMapping 或 @DeleteMapping 等进行注释。

@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.谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在具有工厂方法的spring MVC控制器中进行集成测试? - How to do integrating test in a spring MVC controller which has factory method? 如何序列化 object 它的方法有多个参数 - How to serialize an object which it's method has multiple parameters 如何测试类方法是否正在从类的私有对象之一调用另一个方法 - How can I test if a class method is calling another method from one of the classes's private objects 如何在 @ComponentScan @Configuration 中使用数据源依赖项对 Spring 引导应用程序的 controller 进行单元测试 - How do I unit-test the controller of a Spring Boot application with a DataSource dependency in a @ComponentScan @Configuration 如何为具有 Post 方法的控制器类编写单元测试? - How do I write a unit test for a controller class that has a Post method? 如何调用以Graphics g作为参数的方法? - How do I call a method which has Graphics g as its parameters? 多个参数合并为一个Spring控制器参数 - Multiple parameters into one Spring controller argument 如何在 Spring Boot 控制器中检索查询参数? - How do I retrieve query parameters in a Spring Boot controller? 如何在 spring Controller 中的方法之间传递参数? - How do i pass parameters between methods in a spring Controller? java - 如何在不使用replace方法的情况下替换java中的子字符串以及该函数是否具有多个参数? - How do I replace a substring in java without using the replace method and if the function has multiple parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM