Я пытаюсь получить по запросу некоторые параметры из свойств моего приложения.
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
Я пробую разные варианты:
@RestController
public class HelloController
Первый:
@Autowired
private ApplicationConfig applicationConfig;
@ResponseBody
@GetMapping(value = "/", produces = "application/json")
public ApplicationConfig index() {
return applicationConfig;
}
Исключение:
{"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":"/"}
Второе:
@ResponseBody
@GetMapping(value = "/", produces = "application/json")
public ApplicationConfig index() {
return new ApplicationConfig();
}
// return nothing {"name": null, "ip": null}
Третье:
@ResponseBody
@GetMapping(value = "/", produces = "application/json")
public String index() {
return applicationConfig.getIp();
}
// return x.x.x.x
Мой 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>
Я хочу свойства приложения как объект, что-то вроде:
{"name": "dev", "ip": "x.x.x.x"}