Reputation: 11
This is my controller class, it compile and run normally but doing the put request from Postman I get the 405 error. I have the POST and GET method working fine but the PUT and DELETE, not. url is 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);
}
The output must be this: { "timestamp": "2019-11-22T12:53:32.290+0000", "status": 405, "error": "Method Not Allowed", "message": "Request method 'PUT' not supported", "trace": "org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported\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\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.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.FormContentFilter.doFilterInternal(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.doFilterInternal(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\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)\n\tat 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.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n\tat java.base/java.lang.Thread.run(Thread.java:835)\n", "path": "/tweets" }
Upvotes: 1
Views: 1406
Reputation: 27
In my case I made a typo in the path name. It was @DeleteMapping("/department/{id}")
but should've been @DeleteMapping("/departments/{id}")
Upvotes: 0