如何将参数传递给 Spring Boot REST 控制器? [英] How to pass an argument to a Spring Boot REST Controller?
问题描述
我正在尝试将参数传递给我的 @RESTController Spring Boot 类.
I am trying to pass an argument to my @RESTController Spring Boot class.
在@POSTMapping 方法中,我想使用自定义Java 类的方法来处理接收到的正文并返回响应.
In the @POSTMapping method I want to use a method of a self defined Java class for processing the received body and returning a response.
Spring 应用程序在 Application.java 中启动.控制器对象似乎是隐式创建的.
The Spring application is launched in Application.java. The Controller-Object seems to get created implicitly.
我已经尝试向我的 RESTController 类添加构造函数.但我找不到用参数调用构造函数的方法.
I already tried adding a constructor to my RESTController class. But I couldn't find a way to call that constructor with an argument.
// Application.java
public static void main (String[] args) {
SpringApplication.run(Application.class, args);
}
//ConnectorService.java
@RestController
public class ConnectorService {
private Solveable solver;
public ConnectorService() {}
public ConnectorService (Solveable solveable) {
this.solver = solveable;
}
@CrossOrigin(origins = "http://localhost:3000")
@PostMapping(path = "/maze")
public Solution Test(@RequestBody Test test) {
return solver.solve(test);
}
}
即使我可以定义第二个构造函数,我也没有找到用我的对象调用它的任何方法.
Even though i could define a second constructor, i didn't find any way to call it with my Object.
推荐答案
使用 @RequestParam 传递参数的注解
Use @RequestParam annotation to pass an argument
这篇关于如何将参数传递给 Spring Boot REST 控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!