Java Resteasy代理映射/反序列化
我尝试将我的Resteasy REST客户端(3.0.8)切换到使用代理机制,因为这对我来说非常好。我可以成功地触发REST请求,但现在我需要映射/反序列化响应。下面是代码: RestRequest.java:Java Resteasy代理映射/反序列化,java,json,rest,jackson,resteasy,Java,Json,Rest,Jackson,Resteasy,我尝试将我的Resteasy REST客户端(3.0.8)切换到使用代理机制,因为这对我来说非常好。我可以成功地触发REST请求,但现在我需要映射/反序列化响应。下面是代码: RestRequest.java: ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyWebTarget target = client.target("http://127.0.0.1:8080/backend-1.0-SNAPSHO
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://127.0.0.1:8080/backend-1.0-SNAPSHOT/");
CategoryClient categoryClient = target.proxy(CategoryClient.class);
categoryList = categoryClient.getCategories();
CategoryClient.java(代理接口):
现在我需要一种机制来将响应映射到列表
,目前它只给出一个
javax.ws.rs.client.ResponseProcessingException: javax.ws.rs.ProcessingException:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of
java.util.ArrayList out of START_OBJECT token
映射工作需要哪些步骤?Jackson注释仅在应用于反序列化的类时有效。实现目标的最简单方法是创建表示响应结构的类层次结构:
@JsonIgnoreProperties(ignoreUnknown = true)
class Response {
private Embedded embedded;
// getters and setters here
}
@JsonIgnoreProperties(ignoreUnknown = true)
class Embedded {
private List<Category> categories;
// getters and setters here
}
@JsonIgnoreProperties(ignoreUnknown = true)
class Category {
private String name;
// getters and setters here
}
@JsonIgnoreProperties(ignoreUnknown=true)
班级反应{
私有嵌入式;
//这里有接球手和接球手
}
@JsonIgnoreProperties(ignoreUnknown=true)
嵌入类{
私人名单类别;
//这里有接球手和接球手
}
@JsonIgnoreProperties(ignoreUnknown=true)
类别{
私有字符串名称;
//这里有接球手和接球手
}
然后您将能够获得类别名称。我也在寻找转换选项,但似乎不可能。
我发现最好的方法是使用@JsonDeserialize注释重新构造您的响应:
@JsonDeserialize(converter = ResponseConverter.class)
class Response {
private List<String> categories;
// getters and setters
}
class ResponseConverter implements Converter<Map<String, ?>, Response> {
Response convert(Map<String, ?> jsonInput) {
// convert jsonInput to response and return
}
// implement getInputType and getOutputType methods here
}
@JsonDeserialize(converter=ResponseConverter.class)
班级反应{
私人名单类别;
//接球手和接球手
}
类ResponseConverter实现转换器{
响应转换(映射jsonInput){
//将jsonInput转换为响应并返回
}
//在这里实现getInputType和getOutputType方法
}
assetList看起来不像json中的数组。这是spring数据rest的输出。如果我请求assetList资源,我确实会得到一个具有类似JSON布局的数组。那么,为什么您要尝试解析一个不同于它从输出中给出的格式呢?我不明白。两种情况下的格式都是JSON。spring的数据放在服务器端,显然它只是嵌入的。客户端应用程序不是spring应用程序。它依赖于Resteasy和fasterxml.jackson。如果我把这项工作放在一起,它将节省大量的样板代码。
javax.ws.rs.client.ResponseProcessingException: javax.ws.rs.ProcessingException:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of
java.util.ArrayList out of START_OBJECT token
@JsonIgnoreProperties(ignoreUnknown = true)
class Response {
private Embedded embedded;
// getters and setters here
}
@JsonIgnoreProperties(ignoreUnknown = true)
class Embedded {
private List<Category> categories;
// getters and setters here
}
@JsonIgnoreProperties(ignoreUnknown = true)
class Category {
private String name;
// getters and setters here
}
@JsonDeserialize(converter = ResponseConverter.class)
class Response {
private List<String> categories;
// getters and setters
}
class ResponseConverter implements Converter<Map<String, ?>, Response> {
Response convert(Map<String, ?> jsonInput) {
// convert jsonInput to response and return
}
// implement getInputType and getOutputType methods here
}