Spring Boot Post请求接收默认值而非传入值问题求助
我是Spring Boot新手,开发的Post请求接口始终接收默认值,而非Postman请求体中传入的实际值,请求帮助。以下是相关代码及Postman请求内容:
Postman传入的JSON
{ "email": "Katie", "pass": "UnicornPoop", "city": "", "hairstyleType": "color" }
Controller代码
package com.example.Cosmo.Budget.Tracker.controllers; import com.example.Cosmo.Budget.Tracker.Models.WebUser; import com.example.Cosmo.Budget.Tracker.WebUserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController public class CosmoController { //webUserRepository is an instance variable that allows me to access the records in my table. // WebUserRepository is the class/interface that communicates with Database/Table @Autowired private WebUserRepository webUserRepository; @PostMapping("/addusers") public String addWebUser(@RequestParam(value ="email", defaultValue = "user1") String email, @RequestParam(value = "pass", defaultValue="Rih") String pass, @RequestParam(value = "hairstyleType", defaultValue="shampoo")String hairstyleType, @RequestParam(value = "city", defaultValue="Tampa")String city ) { WebUser webUser = new WebUser(); webUser.setUsername(email); webUser.setPassword(pass); webUser.setCity(city); webUser.setHairstyle(hairstyleType); webUserRepository.save(webUser); return "Added new users to repo!"; } @GetMapping("/getusers") public Iterable<WebUser> getWebUser() { return webUserRepository.findAll(); } }
Model代码
package com.example.Cosmo.Budget.Tracker.Models; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class WebUser { @Id @GeneratedValue private Long id; private String email; private String pass; private String hairstyleType; private String city; public void setId(Long id) { this.id = id; } public WebUser(String email, String pass, String hairstyleType, String city ) { this.email = email; this.pass = pass; this.hairstyleType = hairstyleType; this.city = city; } public WebUser(){ } public String getUsername() { return email; } public void setUsername(String email) { this.email = email; } public String getPassword() { return pass; } public void setPassword(String pass) { this.pass = pass; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getHairstyle() { return hairstyleType; } public void setHairstyle(String hairstyleType) { this.hairstyleType = hairstyleType; } public Long getId() { return id; } @Override public String toString() { return "webUser{" + "id=" + id + ", username='" + email + '\'' + ", password='" + pass + '\'' + ", hairstyleType='" + hairstyleType + '\'' + ", city='" + city + '\'' + '}'; } }
应用启动类代码
package com.example.Cosmo.Budget.Tracker; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class CosmoApplication { public static void main(String[] args) { SpringApplication.run(CosmoApplication.class, args); } }
Repository代码
package com.example.Cosmo.Budget.Tracker; import com.example.Cosmo.Budget.Tracker.Models.WebUser; import org.springframework.data.repository.CrudRepository; public interface WebUserRepository extends CrudRepository<WebUser, Long> { }
问题原因及解决方案
核心问题
你当前使用@RequestParam接收参数,该注解用于获取URL查询参数或表单提交参数,但你在Postman中是通过JSON请求体传递参数,两者不匹配,导致接口只能读取到默认值。
修复步骤
修改Controller方法参数:将
@RequestParam替换为@RequestBody,直接接收WebUser对象,Spring会自动完成JSON请求体到实体类的映射。修改后的Controller方法如下:@PostMapping("/addusers") public String addWebUser(@RequestBody WebUser webUser) { webUserRepository.save(webUser); return "Added new users to repo!"; }无需手动创建WebUser对象并赋值,
@RequestBody会自动完成数据绑定。规范实体类的Getter/Setter(可选但建议):你的实体类中存在属性名与Getter/Setter名称不一致的情况(如
email属性对应getUsername()),虽然不影响映射,但为符合JavaBean规范,建议将getUsername()改为getEmail()、setUsername()改为setEmail(),getPassword()改为getPass()、setPassword()改为setPass()。校验Postman请求设置:确保Postman请求头中
Content-Type设置为application/json,请求体选择raw->JSON格式,保证Spring能正确解析JSON数据。
验证
修改后重启应用,用Postman发送原JSON请求,接口即可正确接收请求体中的实际值。
内容的提问来源于stack exchange,提问作者Mimi

