Spring Boot Custom starter - Set properties

919 Views Asked by At

I have a custom Spring Boot starter.

@Configuration
public class MyAutoConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(MyAutoConfiguration.class);

    @Bean
    public ApiControllerAdvice apiControllerAdvice() {
        logger.info("ApiControllerAdvice created...");
        return new ApiControllerAdvice();
    }

}

and

@RestControllerAdvice
public class ApiControllerAdvice {

    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<ApiError> handleException(Exception exception) {
        HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        return new ResponseEntity<>(new ApiError(httpStatus, exception.getMessage()), httpStatus);
    }
    @ExceptionHandler(value = NotFoundException.class)
    public ResponseEntity<ApiError> handleNotFoundException(NotFoundException exception) {
        HttpStatus httpStatus = HttpStatus.NOT_FOUND;
        return new ResponseEntity<>(new ApiError(httpStatus, exception.getMessage()), httpStatus);
    }

}

It is working fine in the application where I'm using the starter. But in that application I also added in my application.properties : spring.mvc.log-resolved-exception=false

How can I add that property via the starter ? I tried with the starter application.properties and also with @ConfigurationProperties on the MyAutoConfiguration class and apiControllerAdvice bean but nothing seems working.

1

There are 1 best solutions below

6
Gary Liao On

First, which is not relevant to the problem, but answered to default value of spring.mvc.log-resolved-exception.

spring.mvc.log-resolved-exception default is false, check if you have devtools, cause devtools will set spring.mvc.log-resolved-exception into true. I saw this here, and confirmed myself.

Secondly, if both .properties in your custom starter and application that included custom starter are named as application.properties will cause your properties set in custom starter not working since both properties have same name and in same path.

Simplest Solution provided by @Dmitry lonash here, is to name your custom starter properties file into another name, say 'custom.properties', then use @PropertySource(value = {"classpath:application.properties", "classpath:custom.properties"}) to have properties loaded.