Skip to content

How can I map validation errors? #486

@cj19

Description

@cj19

I would like to override the default validation errors, but when I create a basic ControllerAdvice the handler does not get called.
My handler:


@ControllerAdvice
public class DefaultExceptionHandler implements ProblemHandling {

    @Override
    @ExceptionHandler(value = { ConstraintViolationException.class })
    public ResponseEntity<Problem> handleConstraintViolation(ConstraintViolationException exception, NativeWebRequest request) {
        return null;
    }
}

My endpoint:


    @PostMapping(value = "/login", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<JwtResponse> login(@Valid @RequestBody LoginRequest loginRequest) {
        return authService.login(loginRequest);
    }

My security handler advice:

@ControllerAdvice
public class SecurityExceptionHandler implements SecurityAdviceTrait {
}

My websecurity config:


@Configuration
@EnableMethodSecurity
@Import(SecurityProblemSupport.class)
public class WebSecurityConfig {

    @Autowired
    private SecurityProblemSupport problemSupport;
....


    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.cors(cors -> cors.configurationSource(corsConfigurationSource()))
                .csrf(AbstractHttpConfigurer::disable)
                .exceptionHandling(exception -> exception.authenticationEntryPoint(problemSupport)
                        .accessDeniedHandler(problemSupport))
                .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                .authorizeHttpRequests(auth -> auth.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                        .requestMatchers("/api/auth/**").permitAll().anyRequest().authenticated());

        http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);

        return http.build();
    }
}

What am I doing wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions