英文:
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类:
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 +
'}';
}
}
英文:
I'm trying to send a PUT request to my server from Postman, but i keep getting a status 400 Bad Request error, but with POST it works as expected.
When I change the annotation @PutMapping to @PostMapping, it works without changing anything else.
My Controller code :
@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);
}
}
The request I send from 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 +
'}';
}
}
I'm getting in my console :
答案1
得分: 0
尝试将 @RequestBody 添加到 ArticleDTO。
英文:
Try adding @RequestBody to ArticleDTO.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论