Spring Boot Actuator 2.x @WriteOperation 以 object 作为参数
Spring Boot Actuator 2.x @WriteOperation with an object as parameter
我努力实现的目标:
我想创建一个可从 Web 和 jmx 访问的端点,其操作采用 object 作为参数
这是一个简单的例子:
pom.xml
.....
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Object class:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Dog {
protected String name;
protected int age;
}
端点class
@Component
@Endpoint(id = "dogs")
@Slf4j
public class EndpointDogsExperiment {
protected List<Dog> dogs = new ArrayList<>();
public EndpointDogsExperiment() {
dogs.add(new Dog("dog0", 5));
dogs.add(new Dog("dog1", 7));
log.debug("dogs created {}", dogs.toString());
}
@WriteOperation
public List<Dog> addDog(Dog dog) {
log.debug("adding a dog \n{}", dog );
dogs.add(dog);
return dogs;
}
}
问题:
我怎么"call"这个操作
当我尝试使用 HTTP POST
/actuator/dogs
body
{"dog":{"name":"aaaa", "age":33}}
客户得到
回应:
状态:400
日期:2018 年 3 月 31 日星期六 14:59:41 GMT
连接:关闭
transfer-encoding:分块
content-type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8
{"timestamp":"2018-03-31T14:59:41.778+0000","status":400,"error":"Bad Request","message":"No message available","path":"/actuator/dogs"}
和服务器日志说
2018-03-31 17:59:41.766 WARN 12828 --- [nio-8081-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver:无法读取 HTTP 消息:org.springframework.http.converter.HttpMessageNotReadableException: JSON 解析错误:无法从 START_OBJECT 令牌中反序列化 java.lang.String
的实例;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_OBJECT 令牌中反序列化 java.lang.String
的实例
在 [来源:(PushbackInputStream);行:1,列:8](通过引用链:java.util.LinkedHashMap["dog"])
我尝试创建一个带有字符串参数的@WriteOperation,然后
HTTP POST body
{"paramName":"StringParamValue"}
工作正常
这是为什么?
谢谢!
端点操作不支持复杂输入,例如您的 Dog
类型。但是,您可以使用单独的 name
和 age
参数并在操作的实现中创建 Dog
:
@WriteOperation
public List<Dog> addDog(String name, int age) {
Dog dog = new Dog(name, age);
log.debug("adding a dog \n{}", dog );
dogs.add(dog);
return dogs;
}
您可以在 Spring Boot's reference documentation 中了解更多信息。
我努力实现的目标: 我想创建一个可从 Web 和 jmx 访问的端点,其操作采用 object 作为参数
这是一个简单的例子:
pom.xml
.....
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Object class:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Dog {
protected String name;
protected int age;
}
端点class
@Component
@Endpoint(id = "dogs")
@Slf4j
public class EndpointDogsExperiment {
protected List<Dog> dogs = new ArrayList<>();
public EndpointDogsExperiment() {
dogs.add(new Dog("dog0", 5));
dogs.add(new Dog("dog1", 7));
log.debug("dogs created {}", dogs.toString());
}
@WriteOperation
public List<Dog> addDog(Dog dog) {
log.debug("adding a dog \n{}", dog );
dogs.add(dog);
return dogs;
}
}
问题:
我怎么"call"这个操作
当我尝试使用 HTTP POST /actuator/dogs body {"dog":{"name":"aaaa", "age":33}}
客户得到
回应: 状态:400 日期:2018 年 3 月 31 日星期六 14:59:41 GMT 连接:关闭 transfer-encoding:分块 content-type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8
{"timestamp":"2018-03-31T14:59:41.778+0000","status":400,"error":"Bad Request","message":"No message available","path":"/actuator/dogs"}
和服务器日志说
2018-03-31 17:59:41.766 WARN 12828 --- [nio-8081-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver:无法读取 HTTP 消息:org.springframework.http.converter.HttpMessageNotReadableException: JSON 解析错误:无法从 START_OBJECT 令牌中反序列化 java.lang.String
的实例;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_OBJECT 令牌中反序列化 java.lang.String
的实例
在 [来源:(PushbackInputStream);行:1,列:8](通过引用链:java.util.LinkedHashMap["dog"])
我尝试创建一个带有字符串参数的@WriteOperation,然后 HTTP POST body
{"paramName":"StringParamValue"}
工作正常
这是为什么?
谢谢!
端点操作不支持复杂输入,例如您的 Dog
类型。但是,您可以使用单独的 name
和 age
参数并在操作的实现中创建 Dog
:
@WriteOperation
public List<Dog> addDog(String name, int age) {
Dog dog = new Dog(name, age);
log.debug("adding a dog \n{}", dog );
dogs.add(dog);
return dogs;
}
您可以在 Spring Boot's reference documentation 中了解更多信息。