Reactjs 简易弹簧靴+;反应成分

Reactjs 简易弹簧靴+;反应成分,reactjs,spring-boot,Reactjs,Spring Boot,我有一个带有react的webapp,还有一个带有springboot的后端webapp 我在APP.js上使用这个 constructor(props) { super(props); this.state = { greeting: "This is a false greeting" } } componentDidMount() { fetch("/test2").then(function(re

我有一个带有react的webapp,还有一个带有springboot的后端webapp

我在APP.js上使用这个

constructor(props) {
      super(props);

      this.state = {
        greeting: "This is a false greeting" 
      }
    } 

    componentDidMount() {
        fetch("/test2").then(function(response) {
               return response.text();
            }).then((text) => {
                  this.setState({greeting: text})
               });
     }
在控制器上:

@RestController
public class HelloController {

    @RequestMapping("/test")
    public String test() {
        int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
        return "This is a greeting from spring";
    }
它可以工作,使用{this.state.greeting}

我可以看到“这是来自spring的问候语”

我想从app.js发送一个值(一个用按钮选择的数字) 对于弹簧侧控制器,控制器将获取该值,选择一个随机值,比较两个值,并发送响应

我被困在“从app.js向spring-side控制器发送值”的问题上


有人能帮我吗

将查询参数附加到UI上GET请求中的URL

constructor(props) {
  super(props);

  this.state = {
    greeting: "This is a false greeting",
    selectedNumber: 0 //update this state variable on button click
  }

} 

componentDidMount() {
    fetch("/test2?selectedNumber=" + this.state.selectedNumber).then(function(response) {
           return response.text();
        }).then((response) => {
              console.log(response);
           });
 }
在SpringBootAPI控制器上,添加RequestParam,以便API能够从请求中读取查询参数

@RestController
public class HelloController {

@RequestMapping("/test2")
public String test2(@RequestParam("selectedNumber") int selectedNumber) {
    System.out.println(selectedNumber);
    int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
    return "This is a greeting from spring";
}

谢谢,太好了!