I try to get per request some parameters from my application properties.
My ApplicationConfig-Class:
@Configuration
@ConfigurationProperties("org.a.b")
public class ApplicationConfig implements Serializable {
private String name;
private String ip;
// GETTER AND SETTER
My application.properties:
org.a.b.name=huhu
org.a.b.ip=x.x.x.x
I try different possibilies:
@RestController
public class HelloController
First:
@Autowired
private ApplicationConfig applicationConfig;
@ResponseBody
@GetMapping(value = "/", produces = "application/json")
public ApplicationConfig index() {
return applicationConfig;
}
Exception:
{"timestamp":"2018-08-24T12:28:50.623+0000","status":500,"error":"Internal Server Error","message":"Type definition error: [simple type, class org.springframework.context.expression.StandardBeanExpressionResolver]; nested exception is com.fasterxml.jackson.datab
ind.exc.InvalidDefinitionException: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (
through reference chain: hello.bootstrap.ApplicationConfig$$EnhancerBySpringCGLIB$$31a0e3d2[\"$$beanFactory\"]->org.springframework.beans.factory.support.DefaultListableBeanFactory[\"beanExpressionResolver\"])","path":"/"}
Second:
@ResponseBody
@GetMapping(value = "/", produces = "application/json")
public ApplicationConfig index() {
return new ApplicationConfig();
}
// return nothing {"name": null, "ip": null}
Third:
@ResponseBody
@GetMapping(value = "/", produces = "application/json")
public String index() {
return applicationConfig.getIp();
}
// return x.x.x.x
My pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>hello.ConfigServiceApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
I want the Application properties as Object, something like:
{"name": "dev", "ip": "x.x.x.x"}