Good morning every one, I'm having a hard time with this issue. I could work around but I want to understand why this is happening. So far I'm working with Spring, Spring Security, Data Rest and Data Mongo DB.Until now, there has been no issue with DAO and controllers, everything seems to work fine. I've added a new Document:
@Documentpublic class PlanDeEntrenamiento { @Id private String id; private String nombre; private String objetivo; private String descripcion; private List<SesionDePlan> sesiones; private String autor;}
This one is the DAO:
@RepositoryRestResource(path = "planes", itemResourceRel = "plan", collectionResourceRel = "planes")public interface PlanDeEntrenamientoDAO extends MongoRepository<PlanDeEntrenamiento, String> { List<PlanDeEntrenamiento> findByNombre(String nombre); List<PlanDeEntrenamiento> findByObjetivo(String objetivo);}
And this the Security Configuration:
@Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { return http.csrf(csrf -> csrf.disable()) .authorizeHttpRequests(authRequest -> authRequest .requestMatchers("/api/autenticacion/cambioPassword").authenticated() .requestMatchers("/api/autenticacion/login","/api/autenticacion/registro/**","/api/autenticacion/reset-password").permitAll() .requestMatchers(HttpMethod.GET, "/api/invitaciones").authenticated() .requestMatchers(HttpMethod.GET,"/api/invitaciones/**").permitAll() .requestMatchers("/api/fichas/aprobado").permitAll() .requestMatchers(HttpMethod.GET, "/api/fichas/**").permitAll() .requestMatchers(HttpMethod.GET, "/api/ejercicios").permitAll() .requestMatchers(HttpMethod.GET, "/api/planes").permitAll() .requestMatchers(HttpMethod.GET, "/api/planes/**").permitAll() .requestMatchers(HttpMethod.GET, "/api/ejercicios/**").permitAll() .requestMatchers(HttpMethod.GET, "/api/equipamientos").permitAll() .requestMatchers(HttpMethod.GET, "/api/usuarios/existe-email").permitAll() .requestMatchers(HttpMethod.GET, "/api/usuarios/search/existsByNombre").permitAll() .anyRequest().authenticated()) .sessionManagement(sessionManager -> sessionManager.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .authenticationProvider(this.PROVIDER) .addFilterBefore(this.FILTER, UsernamePasswordAuthenticationFilter.class) .build(); }
Now, I create a controller:
@RestController@RequestMapping("api/planes")public class PlanDeEntrenamientoController { @Autowired PlanDeEntrenamientoDAO planDeEntrenamientoDAO; @Autowired SesionDAO sesionDAO; @GetMapping public ResponseEntity<List<PlanDeEntrenamientoResponse>> getPlanes() { List<PlanDeEntrenamiento> planes = planDeEntrenamientoDAO.findAll(); List<PlanDeEntrenamientoResponse> planesResponse = new ArrayList<>(); planesResponse = planes.stream().map(plan -> { PlanDeEntrenamientoResponse planResponse = new PlanDeEntrenamientoResponse(); planResponse.setId(plan.getId()); planResponse.setNombre(plan.getNombre()); planResponse.setObjetivo(plan.getObjetivo()); planResponse.setDescripcion(plan.getDescripcion()); planResponse.setAutor(plan.getAutor() != null ? plan.getAutor() : null); return planResponse; }).collect(Collectors.toList()); return ResponseEntity.ok(planesResponse); } @GetMapping("/{id}") public ResponseEntity<PlanDeEntrenamiento> getPlanes(@PathVariable String id) { PlanDeEntrenamiento planDeEntrenamiento = planDeEntrenamientoDAO.findById(id).orElseThrow(); return ResponseEntity.ok(planDeEntrenamiento); } @PostMapping("/{id}/agregarSesiones") public ResponseEntity<Boolean> getPlanes(@PathVariable String id, @RequestBody AgregarSesionesRequest request) { PlanDeEntrenamiento plan = planDeEntrenamientoDAO.findById(id).orElseThrow(); plan.getSesiones().forEach(sesion -> { Sesion nuevaSesion = new Sesion(); nuevaSesion.setNombre(sesion.getNombre()); nuevaSesion.setFichas(sesion.getFichas()); nuevaSesion.setGrupo(request.getGrupo()); LocalDate fechaSesion = request.getFecha().plusDays(sesion.getDia() - 1); nuevaSesion.setFecha(fechaSesion); nuevaSesion.setUnidad(request.getUnidad()); sesionDAO.save(nuevaSesion); }); return ResponseEntity.ok(true); }}
And after that, when I want to post, put or patch something with the endpoints provided by data rest, the log is this one:Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' is not supported]
Of course I can manually define the endopoints but the point using data rest is not having to do that and I've done nothing different from others Controllers, where I have no problems at all. Any tip would be appreciated.
Thanks.