解决:Required request body is missing

报错信息:
        WARN DefaultHandlerExceptionResolver:384 - Failed to read HTTP message: 
        org.springframework.http.converter.HttpMessageNotReadableException: 
        Required request body is missing: 
        public com.zbz.model.User com.zbz.controller.UserController.saveUser(com.zbz.model.User)

本例场景:
    使用Postman工具访问http://127.0.0.1:8080/spring-common/users/save 
    对应后台方法是如下代码

@Controller
@RequestMapping("/users")
public class UserController {
    @GetMapping("/save")
    public User saveUser(@RequestBody User user) {
        User user2 = user;
        System.out.println(user2.toString());
        return user2;
    }
}

解析:
    在后台方法参数上添加@RequestBody时,将请求参数封装成User对象时,需要JSON(application/json)格式
解决:
    在Postman中如下设置即可正常访问,便于调试。解决:Required request body is missing_第1张图片
实例对象

public class User implements Serializable {

	private static final long serialVersionUID = -6525908145032868837L;
    private Integer id;
	private String username;
	private String password;
	private String nickname;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	@Override
	public String toString() {
		return "User [id=" + id 
				+ ",username=" + username 
				+ ",password=" + password 
				+ ",nickname=" + nickname 
				+ "]";
	}
}

以上,TKS.

你可能感兴趣的:(常见问题)