Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

java - Required request body is missing, but it's there

I have a POST endpoint with a UserStateDTO request body. The Junit test are working fine; however, I cannot make it work from the browser because "Required request body is missing". As you can see the body is there... It's driving me crazy. Please find the error and snippets below.

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public void .....UserRestController.setState(UserStateDTO)
@PostMapping(value = "/state", consumes = MediaType.APPLICATION_JSON_VALUE)
public void setState(@RequestBody UserStateDTO userStateDTO) {
    if (userStateDTO != UserStateDTO.ACTIVE && userStateDTO != UserStateDTO.DISABLED) {
        throw new BusinessException(GeneralErrors.INVALID_REQUEST);
    }
    UserDTO userDTO = userService.getUser();
    userDTO.setState(userStateDTO);
    userService.updateUser(userDTO);
}
public enum UserStateDTO {
    ACTIVE("ACTIVE"),
    DISABLED("DISABLED");

    private final String state;

    private static final Map<String, UserStateDTO> states = new HashMap<>();

    UserStateDTO(String state) {
        this.state = state;
    }

    static {
        for (UserStateDTO u : UserStateDTO.values()) {
            states.put(u.getValue(), u);
        }
    }

    @JsonValue
    public String getValue() {
        return state;
    }

    @JsonCreator
    public static UserStateDTO fromValue(String state) {
        return states.get(StringUtils.upperCase(state));
    }

}

fetch("http://localhost:4200/user/state", {
  "headers": {
    "accept": "application/json, text/plain, */*",
    "accept-language": "en,hu;q=0.9,nb;q=0.8",
    "content-type": "application/json",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "x-xsrf-token": "-"
  },
  "referrer": "http://localhost:4200/",
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": "{"state":"ACTIVE"}",
  "method": "POST",
  "mode": "cors",
  "credentials": "include"
});
question from:https://stackoverflow.com/questions/66066833/required-request-body-is-missing-but-its-there

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to add getState() method inside your enum. Correct code below:

public enum UserStateDTO {
  ACTIVE("ACTIVE"),
  DISABLED("DISABLED");

  private final String state;

  private static final Map<String, UserStateDTO> states = new HashMap<>();

  UserStateDTO (String state) {
    this.state = state;
  }

  static {
    for (UserStateDTO u : UserStateDTO.values()) {
        states.put(u.getValue(), u);
    }
  }

  public String getState() {
    return state;
  }

  @JsonValue
  public String getValue() {
    return state;
  }

  @JsonCreator
  public static UserStateDTO fromValue(String state) {
    return states.get(StringUtils.upperCase(state));
  }
}

Or you can add just inside method fromValue() annotation @JsonProperty("state"), which bind your request body value without getter. Correct method below:

  @JsonCreator
  public static UserStateDTO fromValue(@JsonProperty("state") String state) {
    return states.get(StringUtils.upperCase(state));
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...