0

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.

tweetysat
  • 2,187
  • 14
  • 38
  • 75
  • 1
    Isn't the `spring.mvc.log-resolved-exception` default to false already ? And how do you verify the properties you set aren't working ? – Gary Liao Oct 11 '22 at 08:29
  • No the property is true by default. And I can see it the the log : `2022-10-11 10:38:27.296 WARN 1528 --- [nio-8092-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [com.lab.exception.NotFoundException: My error message]` But my question is also valid for any other property. – tweetysat Oct 11 '22 at 08:40
  • Does this answer your question? [Is it possible to have a default application.yml in a custom Spring boot starter?](https://stackoverflow.com/questions/59092672/is-it-possible-to-have-a-default-application-yml-in-a-custom-spring-boot-starter) – Gary Liao Oct 11 '22 at 11:10

1 Answers1

0

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.

Gary Liao
  • 61
  • 6
  • Thanks @Gary Liao. Effectively I'm using devtools. It explains why `spring.mvc.log-resolved-exception` is true. If I put the `application.properties` in `src/main/resources/config` it is working. But it seems it not a good idea. Now I have a `custom.properties` in my `src/main/resources` and `@PropertySource(value = "classpath:custom.properties")` on my `MyAutoConfiguration ` class but it is not working. – tweetysat Oct 12 '22 at 05:47
  • @tweetysat I create a repo in GitHub , which is my custom starter. Take a look. I can have properties worked nicely. [My Github repo for verifying custom starter issue](https://github.com/gary258796/test-springboot-starter) – Gary Liao Oct 12 '22 at 06:25
  • Many thanks. But as I understand you are using the properties through a class `GaryProperties` and inside the starter. My goal is to 'push' properties inside the application using the starter. – tweetysat Oct 12 '22 at 06:49
  • @tweetysat What do 'push' properties inside the application using the starter actually means ? I thought that you want to have some properties set by your custom starter so your application, which is using your custom starter, can also have that properties being set as long as using the starter – Gary Liao Oct 12 '22 at 07:35
  • Yes it is what I want. But not with custom properties. I want my starter to set `spring.mvc.log-resolved-exception=false` or `debug=true` or `logging.level.org.hibernate.SQL=DEBUG` or ... in the application using it. – tweetysat Oct 12 '22 at 07:59
  • @tweetysat just updated my git repo README. In application that is using starter, `System.out.println("resolveException : " + environment.getProperty("spring.mvc.log-resolved-exception"));` does print out the value i set in starter properties. I think one we should notice is to make sure no other dependencies will modify our config.(Like devtools, which will set spring.mvc.log-resolved-exception into true). Then everything is working as we expected. – Gary Liao Oct 12 '22 at 08:30