Request使用POST方法时可以工作,但是使用PUT方法时却收到400错误。

huangapple go评论74阅读模式
英文:

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发送的请求:[Request使用POST方法时可以工作,但是使用PUT方法时却收到400错误。]

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 +
				'}';
	}
}

我在控制台中看到的信息:Request使用POST方法时可以工作,但是使用PUT方法时却收到400错误。

英文:

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(&quot;/articles&quot;)
public class ArticleController {
private static final Logger LOG = LogManager.getLogger(ArticleController.class);
private ArticleService articleService;
@Value(&quot;${articles.default.page_size}&quot;)
private int articlesDefaultPageSize;
@Autowired
public void setArticleService(ArticleService articleService) {
this.articleService = articleService;
}
@PutMapping(&quot;/update&quot;)
public ResponseEntity&lt;Article&gt; update(@ModelAttribute @Validated ArticleDTO articleDTO,
@AuthenticationPrincipal CustomUserDetails currentUser) {
LOG.trace(currentUser.getUsername() + &quot; wants to update an article : &quot; + articleDTO);
Article a = articleService.updateExpertArticle(articleDTO, (Expert) currentUser.getUser());
return ResponseEntity.ok(a);
}
}

The request I send from Postman : Request使用POST方法时可以工作,但是使用PUT方法时却收到400错误。

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(&quot;--------Instantiate ArticleDTO -------&quot;);
}
public MultipartFile[] getImages() {
return images;
}
public void setImages(MultipartFile... images) {
LOG.trace(&quot;--------Set Images-------&quot;);
this.images = images;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
LOG.trace(&quot;--------Set Titles-------&quot;);
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
LOG.trace(&quot;--------Set Content-------&quot;);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
LOG.trace(&quot;--------Set Id-------&quot;);
}
public LocalDateTime getCreationDate() {
return creationDate;
}
public void setCreationDate(LocalDateTime creationDate) {
LOG.trace(&quot;--------Set Date-------&quot;);
this.creationDate = creationDate;
}
@Override
public String toString() {
return &quot;ArticleDTO{&quot; +
&quot;id=&#39;&quot; + id + &#39;\&#39;&#39; +
&quot;, title=&#39;&quot; + title + &#39;\&#39;&#39; +
&quot;, content=&#39;&quot; + content + &#39;\&#39;&#39; +
&quot;, images=&quot; + Arrays.toString(images) +
&quot;, creationDate=&quot; + creationDate +
&#39;}&#39;;
}
}

I'm getting in my console :

Request使用POST方法时可以工作,但是使用PUT方法时却收到400错误。

答案1

得分: 0

尝试将 @RequestBody 添加到 ArticleDTO。

英文:

Try adding @RequestBody to ArticleDTO.

huangapple
  • 本文由 发表于 2020年8月12日 02:52:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63364606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定