Java spring boot - método POST | Fórum | School of Net

Deseja poder participar de nosso fórum e tirar todas as suas dúvidas?
Clique aqui e assine nosso plano de acesso ilimitado. Saiba mais.

por Gilberto

1 semana, 4 dias atrás Gilberto

Java spring boot - método POST

estou montando esse relacionamento de classes no springboot https://prnt.sc/vmguta nesse momento estou na classe funcionarios meu método post no controller ``` package com.cardif.controller; import java.util.List; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import com.cardif.entity.Cargo; import com.cardif.entity.Funcionario; import com.cardif.service.CargoService; import com.cardif.service.FuncionarioService; @RestController @RequestMapping("/funcionarios") public class FuncionarioController { private FuncionarioService funcionarioService; private CargoService cargoService; @Autowired public FuncionarioController(FuncionarioService funcionarioService, CargoService cargoService) { this.funcionarioService = funcionarioService; this.cargoService = cargoService; } @PostMapping("/{funcionario_id}") @ResponseBody @ResponseStatus(HttpStatus.CREATED) public ResponseEntity<Funcionario> create(@Valid @PathVariable(value = "funcionario_id") Long funcionario_id, @RequestBody Funcionario funcionario, Cargo cargo) { this.funcionarioService.create(funcionario_id, funcionario, cargo); return ResponseEntity.status(HttpStatus.CREATED).body(funcionario); } @CrossOrigin @GetMapping @ResponseBody public List<Funcionario> findAll() { return this.funcionarioService.findAll(); } @GetMapping("/{id}") @ResponseBody public Funcionario findById(@PathVariable(value = "id") Long id) { return this.funcionarioService.findById(id); } @PutMapping("/{id}") @ResponseBody public Funcionario update(@Valid @PathVariable(value = "id") Long id, @RequestBody Funcionario funcionario) { return this.funcionarioService.update(id, funcionario); } @DeleteMapping(value = "/{id}") public void delete(@PathVariable(value = "id") Long id, HttpServletResponse response) { this.funcionarioService.delete(id); response.setStatus(HttpServletResponse.SC_NO_CONTENT); } } ``` //////////// e o erro que aparece no postman ``` { "timestamp": "2020-11-19T22:20:38.376+00:00", "status": 405, "error": "Method Not Allowed", "trace": "org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported\r\n\tat org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:250)\r\n\tat org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:417)\r\n\tat org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:364)\r\n\tat org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getHandlerInternal(RequestMappingInfoHandlerMapping.java:123)\r\n\tat org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getHandlerInternal(RequestMappingInfoHandlerMapping.java:66)\r\n\tat org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:491)\r\n\tat org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1255)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1037)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:961)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:652)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:733)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat com.cardif.config.CORSFilter.doFilter(CORSFilter.java:44)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)\r\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\r\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)\r\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)\r\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)\r\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\r\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\r\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat java.base/java.lang.Thread.run(Thread.java:834)\r\n", "message": "Request method 'POST' not supported", "path": "/funcionarios" } ``` ///// agora meu service impl https://prnt.sc/vmgxq6 /// minha classe funcionario ``` package com.cardif.entity; import java.time.LocalDate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.validation.constraints.Past; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonProperty; @Entity @Table(name = "tbl_funcionarios") public class Funcionario { @GeneratedValue(strategy = GenerationType.IDENTITY) @Id private Long funcionario_id; private String funcionario_name; private Integer funcionario_age; @Past @JsonProperty("funcionario_birthday") @JsonFormat(pattern = "dd-MM-yyyy") private LocalDate funcionario_birthday; private String funcionario_document; @ManyToOne private Cargo cargo; public Funcionario() { super(); } public Funcionario(String funcionario_name, Integer funcionario_age, LocalDate funcionario_birthday, String funcionario_document, Cargo cargo) { super(); this.funcionario_name = funcionario_name; this.funcionario_age = funcionario_age; this.funcionario_birthday = funcionario_birthday; this.funcionario_document = funcionario_document; this.cargo = cargo; } public Long getFuncionario_id() { return funcionario_id; } public void setFuncionario_id(Long funcionario_id) { this.funcionario_id = funcionario_id; } public String getFuncionario_name() { return funcionario_name; } public void setFuncionario_name(String funcionario_name) { this.funcionario_name = funcionario_name; } public Integer getFuncionario_age() { return funcionario_age; } public void setFuncionario_age(Integer funcionario_age) { this.funcionario_age = funcionario_age; } public LocalDate getFuncionario_birthday() { return funcionario_birthday; } public void setFuncionario_birthday(LocalDate funcionario_birthday) { this.funcionario_birthday = funcionario_birthday; } public String getFuncionario_document() { return funcionario_document; } public void setFuncionario_document(String funcionario_document) { this.funcionario_document = funcionario_document; } public Cargo getCargo() { return cargo; } public void setCargo(Cargo cargo) { this.cargo = cargo; } @Override public String toString() { return "Funcionario [funcionario_id=" + funcionario_id + ", funcionario_name=" + funcionario_name + ", funcionario_age=" + funcionario_age + ", funcionario_birthday=" + funcionario_birthday + ", funcionario_document=" + funcionario_document + ", cargo=" + cargo + "]"; } } ``` ////// e minha classe Cargo ``` package com.cardif.entity; import java.util.List; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name = "tbl_cargos") public class Cargo { @GeneratedValue(strategy = GenerationType.IDENTITY) @Id private Long cargo_id; @Column private String cargo_name; @OneToMany private List<Funcionario> funcionarios; public Cargo() { super(); } public Cargo(String cargo_name) { super(); this.cargo_name = cargo_name; } public Long getCargo_id() { return cargo_id; } public void setCargo_id(Long cargo_id) { this.cargo_id = cargo_id; } public String getCargo_name() { return cargo_name; } public void setCargo_name(String cargo_name) { this.cargo_name = cargo_name; } public List<Funcionario> getFuncionarios() { return funcionarios; } public void setFuncionarios(List<Funcionario> funcionarios) { this.funcionarios = funcionarios; } @Override public String toString() { return "Cargo [cargo_id=" + cargo_id + ", cargo_name=" + cargo_name + ", funcionarios=" + funcionarios + "]"; } } ```

8 Respostas