Java I';我在运行Spring后端时遇到这个错误:405方法不允许请求方法';把';不支持
这是我的控制器类,它可以正常编译和运行,但是在执行Postman的put请求时,我得到了405错误。我有张贴和获取方法工作良好,但放和删除,而不是。url是Java I';我在运行Spring后端时遇到这个错误:405方法不允许请求方法';把';不支持,java,spring,backend,Java,Spring,Backend,这是我的控制器类,它可以正常编译和运行,但是在执行Postman的put请求时,我得到了405错误。我有张贴和获取方法工作良好,但放和删除,而不是。url是 @CrossOrigin @RestController @请求映射(“/tweets”) 公共类TweetController{ @自动连线 私人推文库推文; 私有tweet存储库; @GetMapping 公共列表列表(){ 返回tweets.findAll(); } @GetMapping(“/{id}”) 公共响应总线车(@Path
@CrossOrigin
@RestController
@请求映射(“/tweets”)
公共类TweetController{
@自动连线
私人推文库推文;
私有tweet存储库;
@GetMapping
公共列表列表(){
返回tweets.findAll();
}
@GetMapping(“/{id}”)
公共响应总线车(@PathVariable Long id){
可选tweet=tweets.findById(id);
if(tweet.isEmpty()){
返回ResponseEntity.notFound().build();
}
返回ResponseEntity.ok(tweet.get());
}
@邮戳
@ResponseStatus(HttpStatus.CREATED)
公共Tweet adicionar(@Valid@RequestBody Tweet){
可选tweetExistente=推文
.findByNomeAndTexto(tweet.getNome(),tweet.getTexto());
if(tweetExistente.isPresent()){
抛出新的ResponseStatusException(HttpStatus.BAD_请求,
“现有数据”);
}
返回tweets.save(tweet);
}
@PutMapping(“/{id}”)
@ResponseStatus(HttpStatus.OK)
public ResponseEntity editar(@RequestBody Tweet,@PathVariable long id){
可选tweetOptional=tweetRepository.findById(id);
如果(!tweetOptional.isPresent())
返回ResponseEntity.notFound().build();
setId(tweet.getId());
tweet.setNome(tweet.getNome());
tweet.setTexto(tweet.getTexto());
tweetRepository.save(tweet);
返回ResponseEntity.noContent().build();
}
@DeleteMapping(“/{id}”)
@ResponseStatus(HttpStatus.OK)
public void deletar(@PathVariable long id){
tweetRepository.deleteById(id);
}
输出必须如下所示:
{
“时间戳”:“2019-11-22T12:53:32.290+0000”,
“地位”:405,
“错误”:“不允许使用方法”,
“消息”:“不支持请求方法”PUT“,
“跟踪”:“org.springframework.web.HttpRequestMethodNotSupportedException:请求方法'PUT'不受支持\n\t org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:201)\n\t org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:421)\n\tat org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:367)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.getHandlerInternal(RequestMappingHandlerMapping.java:449)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.getHandlerInternal(RequestMappingHandlerMapping.java:67)\n\tat org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:393)\n\tat org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1234)\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1016)\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\n\tat org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:920)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:663)\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:741)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\n\tat org.springframework.web.filter.oncePerRequestContextFilter.doFilter(oncePerRequestContextFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.FormContentFilter.doFilter内部(FormContentFilter.java:93)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilter内部(CharacterEncodingFilter.java:201)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\n\t org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:
@CrossOrigin
@RestController
@RequestMapping ("/tweets")
public class TweetController {
@Autowired
private TweetRepository tweets;
private TweetRepository tweetRepository;
@GetMapping
public List<Tweet> listar() {
return tweets.findAll();
}
@GetMapping ("/{id}")
public ResponseEntity<Tweet> buscar(@PathVariable Long id) {
Optional<Tweet> tweet = tweets.findById(id);
if (tweet.isEmpty()) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(tweet.get());
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Tweet adicionar(@Valid @RequestBody Tweet tweet) {
Optional<Tweet> tweetExistente = tweets
.findByNomeAndTexto(tweet.getNome(), tweet.getTexto());
if (tweetExistente.isPresent()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Existing data");
}
return tweets.save(tweet);
}
@PutMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<Object> editar(@RequestBody Tweet tweet, @PathVariable long id) {
Optional<Tweet> tweetOptional = tweetRepository.findById(id);
if (!tweetOptional.isPresent())
return ResponseEntity.notFound().build();
tweet.setId(tweet.getId());
tweet.setNome(tweet.getNome());
tweet.setTexto(tweet.getTexto());
tweetRepository.save(tweet);
return ResponseEntity.noContent().build();
}
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public void deletar(@PathVariable long id) {
tweetRepository.deleteById(id);
}