请求使用 POST 但使用 PUT 我得到 400 错误
Request work with POST but with PUT I got 400 error
我正在尝试从 Postman 向我的服务器发送 PUT 请求,但我一直收到状态 400 Bad Request 错误,但是 POST 它按预期工作。
当我将注释@PutMapping 更改为@PostMapping 时,无需更改任何其他内容即可正常工作。
我的控制器代码:
@RestController
@RequestMapping("/articles")
public class ArticleController {
private static final Logger LOG = LogManager.getLogger(ArticleController.class);
private ArticleService articleService;
@Value("${articles.default.page_size}")
private int articlesDefaultPageSize;
@Autowired
public void setArticleService(ArticleService articleService) {
this.articleService = articleService;
}
@PutMapping("/update")
public ResponseEntity<Article> update(@ModelAttribute @Validated ArticleDTO articleDTO,
@AuthenticationPrincipal CustomUserDetails currentUser) {
LOG.trace(currentUser.getUsername() + " wants to update an article : " + articleDTO);
Article a = articleService.updateExpertArticle(articleDTO, (Expert) currentUser.getUser());
return ResponseEntity.ok(a);
}
}
我从 Postman 发送的请求:
ArticleDTO class :
package org.mql.dto;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Arrays;
public class ArticleDTO {
private static final Logger LOG = LogManager.getLogger(ArticleDTO.class);
private String id;
@NotNull
@NotEmpty
private String title;
@NotNull
@NotEmpty
private String content;
private MultipartFile[] images;
private LocalDateTime creationDate;
public ArticleDTO() {
LOG.trace("--------Instantiate ArticleDTO -------");
}
public MultipartFile[] getImages() {
return images;
}
public void setImages(MultipartFile... images) {
LOG.trace("--------Set Images-------");
this.images = images;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
LOG.trace("--------Set Titles-------");
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
LOG.trace("--------Set Content-------");
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
LOG.trace("--------Set Id-------");
}
public LocalDateTime getCreationDate() {
return creationDate;
}
public void setCreationDate(LocalDateTime creationDate) {
LOG.trace("--------Set Date-------");
this.creationDate = creationDate;
}
@Override
public String toString() {
return "ArticleDTO{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", content='" + content + '\'' +
", images=" + Arrays.toString(images) +
", creationDate=" + creationDate +
'}';
}
}
我正在进入我的控制台:
尝试将@RequestBody 添加到 ArticleDTO。
我正在尝试从 Postman 向我的服务器发送 PUT 请求,但我一直收到状态 400 Bad Request 错误,但是 POST 它按预期工作。
当我将注释@PutMapping 更改为@PostMapping 时,无需更改任何其他内容即可正常工作。
我的控制器代码:
@RestController
@RequestMapping("/articles")
public class ArticleController {
private static final Logger LOG = LogManager.getLogger(ArticleController.class);
private ArticleService articleService;
@Value("${articles.default.page_size}")
private int articlesDefaultPageSize;
@Autowired
public void setArticleService(ArticleService articleService) {
this.articleService = articleService;
}
@PutMapping("/update")
public ResponseEntity<Article> update(@ModelAttribute @Validated ArticleDTO articleDTO,
@AuthenticationPrincipal CustomUserDetails currentUser) {
LOG.trace(currentUser.getUsername() + " wants to update an article : " + articleDTO);
Article a = articleService.updateExpertArticle(articleDTO, (Expert) currentUser.getUser());
return ResponseEntity.ok(a);
}
}
我从 Postman 发送的请求:
ArticleDTO class :
package org.mql.dto;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Arrays;
public class ArticleDTO {
private static final Logger LOG = LogManager.getLogger(ArticleDTO.class);
private String id;
@NotNull
@NotEmpty
private String title;
@NotNull
@NotEmpty
private String content;
private MultipartFile[] images;
private LocalDateTime creationDate;
public ArticleDTO() {
LOG.trace("--------Instantiate ArticleDTO -------");
}
public MultipartFile[] getImages() {
return images;
}
public void setImages(MultipartFile... images) {
LOG.trace("--------Set Images-------");
this.images = images;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
LOG.trace("--------Set Titles-------");
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
LOG.trace("--------Set Content-------");
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
LOG.trace("--------Set Id-------");
}
public LocalDateTime getCreationDate() {
return creationDate;
}
public void setCreationDate(LocalDateTime creationDate) {
LOG.trace("--------Set Date-------");
this.creationDate = creationDate;
}
@Override
public String toString() {
return "ArticleDTO{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", content='" + content + '\'' +
", images=" + Arrays.toString(images) +
", creationDate=" + creationDate +
'}';
}
}
我正在进入我的控制台:
尝试将@RequestBody 添加到 ArticleDTO。