Search code examples
javaspring-bootcorsjhipster

Enabling CORS in Spring Boot JHipster givng "Failed to bind properties" error


To enable CORS in a JHipster Spring Boot app I am uncommenting the settings in application.ym.

jhipster:
  clientApp:
    name: 'modelDfApp'
  # By default CORS is disabled. Uncomment to enable.
    cors:
      allowed-origins: "*"
  #   allowed-methods: "*"
  #   allowed-headers: "*"
  #   exposed-headers: "Authorization,Link,X-Total-Count,X-${jhipster.clientApp.name}-alert,X-${jhipster.clientApp.name}-error,X-${jhipster.clientApp.name}-params"
  #   allow-credentials: true
  #   max-age: 1800

Note: Leaving most lines commented out here to shorted error message.

Intellij does not complain and maven compiles into Docker container no problem. But starting the container gives

2023-06-28 13:54:19 2023-06-28T12:54:19.799Z  WARN 1 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt:
 org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'undertowServletWebServerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedUndertow.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webConfigurer' defined in file [/app/classes/uk/modeldrivers/modeldf/config/WebConfigurer.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'jhipster-tech.jhipster.config.JHipsterProperties': Could not bind properties to 'JHipsterProperties' : prefix=jhipster, ignoreInvalidFields=false, ignoreUnknownFields=false; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'jhipster' to tech.jhipster.config.JHipsterProperties 
2023-06-28 13:54:19 2023-06-28T12:54:19.899Z ERROR 1 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : __***************************_APPLICATION FAILED TO START_***************************__Description:__Binding to target [Bindable@6f930e0 type = tech.jhipster.config.JHipsterProperties, value = 'provided', annotations = array<Annotation>
[@org.springframework.boot.context.properties.ConfigurationProperties(ignoreInvalidFields=false, ignoreUnknownFields=false, prefix="jhipster", value="jhipster")]] failed:__    
Property: jhipster.clientapp.cors.allowed-origins_    Value: "*"_    Origin: class path resource [config/application.yml] - 193:24_    
Reason: The elements [jhipster.clientapp.cors.allowed-origins] were left unbound.__Action:__Update your application's configuration_

Any suggestions gratefully received. Clues as to how you would debug this also very welcome.


Solution

  • Ok, so I made the cors entry the same level as clientApp and it runs.

    Looks like cors is a child of jhipster, not clientApp!

    So

    jhipster:
      clientApp:
        name: 'XXX'
      # By default CORS is disabled. Uncomment to enable.
      cors:
        allowed-origins: 'http://localhost:8080,http://localhost:9000'
    

    not

    jhipster:
      clientApp:
        name: 'XXX'
        # By default CORS is disabled. Uncomment to enable.
        cors:
          allowed-origins: 'http://localhost:8080,http://localhost:9000'