这是我的控制器类,它可以编译并正常运行,但是从Postman发出放置请求时出现405错误。我的POST和GET方法工作正常,但PUT和DELETE却没有。网址为http://localhost:8080/tweets
@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);
}
输出必须是这样的: { “ timestamp”:“ 2019-11-22T12:53:32.290 + 0000”, “状态”:405, “错误”:“方法不允许”, “ message”:“不支持请求方法'PUT'”, “ trace”:“ org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法'PUT'\ n \ tat org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:201)\ n \ tat org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:421)\ n \ t 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(DispatcherSe rvlet.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 \ t 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.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\t org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\ n \ t 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.doFilterInternal(CharacterEncodingFilter.jav a: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 \ t 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 \ t org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\ n \ t org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)\ n \ tatorg.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)\ n \ t org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:861)\ n \ t org.apache.tomcat.util。 net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1579)\ n \ tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\ n \ tat java.base / java.util。 parallel.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\ n \ t java.base / java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:628)\ n \ t org.apache.tomcat.util thread.TaskThread $ WrappingRunnable.run(TaskThread.java:61)\ n \ tat java.base / java.lang.Thread.run(Thread.java:835)\ n“, “ path”:“ / tweets” }