我在 postman 中输入此代码 {"name":"upul","email":"upul@gmail.com"} 作为 post 请求,

I type this code {"name":"upul","email":"upul@gmail.com"} in postman as a post request ,

但是我遇到了这个错误,

2021-02-05 12:19:08.944 WARN 8168 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver:已解决[org.springframework.web.bind.MissingServletRequestParameterException:必需的字符串参数'name'不存在]

依赖关系是,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.sample</groupId>
    <artifactId>springboot-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-test</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

** 存储库是,**

package com.example.sample.springboottest.Model;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Integer id;

    private String name;

    private String email;

    public Integer getId() {
        return id;
    }



    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

** 控制器是,**

package com.example.sample.springboottest.Controller;

import com.example.sample.springboottest.Model.User;
import com.example.sample.springboottest.Repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping(path = "/demo")
public class MainController {

@Autowired
private UserRepository userRepository;

@PostMapping(path = "/add")
public @ResponseBody String addNewUser(@RequestParam String name, @RequestParam String email){

    User n =new User();

    n.setName(name);
    n.setEmail(email);
    userRepository.save(n);
    return "Saved";
}


@GetMapping(path = "/all")
public @ResponseBody Iterable<User> getAllUser(){
    return userRepository.findAll();
}




}

** 属性是,**

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=root
spring.datasource.password=root


spring.jpa.open-in-view=true

spring.jpa.show-sql=true

请帮我解决这个问题,

Required String parameter 'name' is not present

默认需要用@RequestParam注解的方法参数。看起来您在调用 API 时没有传递 name 查询参数。

编辑:在这种情况下,我们可以在请求正文中传递用户。

看来你混淆了 @RequestParam@RequestBody

您似乎想要 POST 一个 JSON 对象到您的控制器,在这种情况下,您的方法签名应该是

public @ResponseBody String addNewUser(@RequestBody User user){

由于您正在使用 @ReuestParam,因此您无法像您所做的那样传递 JSON。 为了传递 JSON 格式,将 @RequestParam 更改为 @RequestBody,如下所示:

    @PostMapping(path = "/add")
    public @ResponseBody String addNewUser(@RequestBody User user){

//        User n =new User();
//
//        n.setName(name);
//        n.setEmail(email);
//        userRepository.save(n);
        userRepository.save(user);
        return "Saved";
    }

邮递员结果如下:

但是如果你需要使用@RequestParam 然后传递查询字符串如下:

localhost:8811/demo/add?name=upul&email=upul@gmail.com