有人能解释下面的密码吗?
我知道RESTEasy客户和泽西AuthenticationFeature..。但是,这意味着SimpleOperation类以及它是什么API?
HttpAuthenticationFeature feature = HttpAuthenticationFeature.digest("admin", "admin");
Client client = ClientBuilder.newClient();
client.register(feature);
Entity<SimpleOperation> operation = Entity.entity(
new SimpleOperation("read-resource", true, "subsystem", "undertow", "server", "default-server"),
MediaType.APPLICATION_JSON_TYPE);
WebTarget managementResource = client.target("http://localhost:9990/management");
String response = managementResource.request(MediaType.APPLICATION_JSON_TYPE)
.header("Content-type", MediaType.APPLICATION_JSON)
.post(operation, String.class);
System.out.println(response);来自:https://docs.jboss.org/author/display/WFLY10/The+HTTP+management+API
发布于 2016-04-21 15:41:20
如果您无法了解SimpleOperation类是什么,或者它只是文档的一些组合类,那么您可以简单地创建自己的类。它只是JSON序列化程序用来序列化到JSON的一个简单的POJO。如果您不熟悉JSON/POJO映射,下面是一些技巧
{ }会映射到
类SomeClassfirstName,那么您将需要一个带有getter和setter的字段,其中getter和setter匹配JSON属性的名称(带有get/set前缀和第一个字母大写)
类SomeClass {私有字符串firstName;公共字符串getFirstName() {返回firstName;}公共空setFirstName(String firstName) { this.firstName = firstName }}
因此,如果要将new SomeClass("Joao")作为实体发送,它将序列化为
{"firstName":"Joao"}尽管如此,如果您知道您需要发送的JSON格式,那么创建您自己的POJO应该不会太困难。
其他一些类型映射:
List。因此,如果您有["hello", "world"],您可以将其映射到List<String>。[{"prop":"value"}, {"prop":"value"}],则可以将其映射到List<SomeType>getProperty、isProperty、hasProperty。这就是我所能想到的最基本的东西。例如,查看您提供的链接中的一个示例请求。
{
"operation":"read-resource",
"include-runtime":"true",
"recursive":"true",
"address":["subsystem","undertow","server","default-server"]
}你可以把它映射到POJO
public class SimpleOperation {
private String operation;
@JsonProperty("include-runtime")
private boolean includeRuntime;
public boolean recursive;
private List<String> address;
public SimpleOperation(String operation, boolean includeRuntime,
boolean recursive, String... address) {
this.operation = operation;
this.includeRuntime = includeRuntime;
this.address = Arrays.asList(address);
}
// getters and setters.
}@JsonProperty使杰克逊序列化程序知道如何设置JSON属性名称。这将是默认的,不符合我前面提到的命名约定,但是它不知道在名称中使用-,所以我们明确地告诉它应该使用什么属性名。
那你就可以
new SimpleOperation("read-resource", true, "subsystem", "undertow", "server", "default-server")它应该被序列化到上面的JSON
https://stackoverflow.com/questions/36772354
复制相似问题