我正在开发一个使用不同API的Web应用程序来从某些站点获取信息。但是其中一个在尝试使用我创建的搜索方法时遇到了这个错误(唯一使用此API的方法)。
org.restlet.resource.ResourceException: OK (406) - The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request
at org.restlet.resource.ClientResource.doError(ClientResource.java:592)
at org.restlet.resource.ClientResource.handleInbound(ClientResource.java:1153)
at org.restlet.resource.ClientResource.handle(ClientResource.java:1008)
at org.restlet.resource.ClientResource.handle(ClientResource.java:946)
at org.restlet.resource.ClientResource.get(ClientResource.java:660)
at aiss.model.resource.KitsuResources.getBusqueda(KitsuResources.java:28)
at aiss.controller.SearchController.doGet(SearchController.java:34)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:128)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:48)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:95)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:508)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
这是控制台在尝试使用该方法时返回的日志。 失败应该在一个名为KitsuResources的类中,当它试图创建类“Example”的对象时: KitsuResources类:
public class KitsuResources {
private static final String URL_Kitsu = "https://kitsu.io/api/edge";
private static final String searchPath = "/anime?filter[text]=%QUERY&page[limit]=5&page[offset]=5&fields[anime]=titles";
public Example getBusqueda(String query) throws UnsupportedEncodingException {
String search = URLEncoder.encode(query, "UTF-8");
ClientResource cr = null;
Example busquedaAnime = null;
try {
cr = new ClientResource(URL_Kitsu + searchPath.replace("%QUERY", search));
busquedaAnime = cr.get(Example.class);
} catch (ResourceException e) {
throw e;
}
return busquedaAnime;
}}
类示例:
package aiss.kitsu;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"data",
"meta",
"links"
})
public class Example {
@JsonProperty("data")
private List<Datum> data = null;
@JsonProperty("meta")
private Meta meta;
@JsonProperty("links")
private Links_ links;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("data")
public List<Datum> getData() {
return data;
}
@JsonProperty("data")
public void setData(List<Datum> data) {
this.data = data;
}
public Example withData(List<Datum> data) {
this.data = data;
return this;
}
@JsonProperty("meta")
public Meta getMeta() {
return meta;
}
@JsonProperty("meta")
public void setMeta(Meta meta) {
this.meta = meta;
}
public Example withMeta(Meta meta) {
this.meta = meta;
return this;
}
@JsonProperty("links")
public Links_ getLinks() {
return links;
}
@JsonProperty("links")
public void setLinks(Links_ links) {
this.links = links;
}
public Example withLinks(Links_ links) {
this.links = links;
return this;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
public Example withAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
return this;
}}
这是启动请愿并获得反序列化响应的控制器:
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import aiss.kitsu.Example;
import aiss.model.resource.KitsuResources;
public class SearchController extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger log = Logger.getLogger(SearchController.class.getName());
public SearchController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String query = request.getParameter("searchQuery");
RequestDispatcher rd = null;
KitsuResources aniL = new KitsuResources();
Example resultado = aniL.getBusqueda(query);
if (resultado!=null){
request.setAttribute("data", resultado);
rd = request.getRequestDispatcher("/resultados.jsp");
} else {
log.log(Level.SEVERE, "Kitsu object: " + resultado);
rd = request.getRequestDispatcher("/error.jsp");
}
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}}
我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>0.0.1</version>
<groupId>aiss</groupId>
<artifactId>AniRat</artifactId>
<!-- Restlet repository -->
<repositories>
<repository>
<id>maven-restlet</id>
<name>Restlet repository</name>
<url>https://maven.restlet.com</url>
</repository>
</repositories>
<properties>
<appengine.version>1.9.46</appengine.version> <!-- AppEngine version -->
<servlet.version>2.5</servlet.version> <!-- Servlet version -->
<jslt.version>1.2</jslt.version> <!-- JSLT version -->
<restlet.version>2.3.7</restlet.version> <!-- Restlet version -->
<resteasy.version>2.3.5.Final</resteasy.version> <!-- RESTEasy version -->
<jackson.version>2.8.2</jackson.version> <!-- Jackson version -->
<google.client.version>1.20.0</google.client.version> <!-- Google HTTP Client version -->
<junit.version>4.12</junit.version> <!-- JUnit version -->
<mockito.version>2.0.2-beta</mockito.version> <!-- Mockito version -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
<archiveClasses>true</archiveClasses>
</properties>
<prerequisites>
<maven>3.3.9</maven>
</prerequisites>
<dependencies>
<!-- Compile/runtime dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>${appengine.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jslt.version}</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<!--Json Dependencies -->
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.json</artifactId>
<version>${restlet.version}</version>
</dependency>
<!-- Jackson 2 core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1.1</version>
</dependency>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet.ext.jackson</artifactId>
<version>${restlet.version}</version>
</dependency>
<!-- RESTEasy -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
<!-- Google HTTP Client Library -->
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>${google.client.version}</version>
</dependency>
<!-- Google HTTP Client Library (AppEngine extension) -->
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-appengine</artifactId>
<version>${google.client.version}</version>
</dependency>
<!-- Jackson module for Google HTTP Client -->
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>${google.client.version}</version>
</dependency>
<!-- Google OAuth Client Library -->
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-servlet</artifactId>
<version>${google.client.version}</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-servlet</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
<build>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<!-- Maven AppEngine plugin -->
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${appengine.version}</version>
<configuration>
<enableJarClasses>false</enableJarClasses>
<version>${app.version}</version>
<!-- Comment in the below snippet to bind to all IPs instead of just
localhost -->
<!-- <address>0.0.0.0</address> -->
<port>8090</port>
<!-- Comment in the below snippet to enable local debugging with a remote
debugger like those included with Eclipse or IntelliJ -->
<jvmFlags>
<jvmFlag>-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n</jvmFlag>
</jvmFlags>
</configuration>
</plugin>
<!-- Hot reload of Java and static files (http://stackoverflow.com/questions/21345177/is-there-any-way-to-trigger-the-maven-appengine-devserver-to-auto-refresh-static) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<property name="target.webapp.dir"
value="${project.build.directory}/${project.build.finalName}" />
<property name="src.webapp.dir" value="${basedir}/src/main/webapp" />
<sync verbose="true" todir="${target.webapp.dir}"
includeEmptyDirs="true">
<fileset dir="${src.webapp.dir}" />
<preserveintarget>
<include name="WEB-INF/lib/**" />
<include name="WEB-INF/classes/**" />
<include name="WEB-INF/appengine-generated/**" />
</preserveintarget>
</sync>
<!-- <sync verbose="true" todir="${target.webapp.dir}/WEB-INF/classes">
<fileset dir="${basedir}/target/classes" /> </sync> -->
</target>
</configuration>
</execution>
</executions>
</plugin>
<!-- Code Coverage report generation -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>generate-code-coverage-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!-- Hot reload of Java and static files (http://stackoverflow.com/questions/21345177/is-there-any-way-to-trigger-the-maven-appengine-devserver-to-auto-refresh-static) -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<versionRange>[1.6,)</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnIncremental>true</runOnIncremental>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
会出现什么问题?似乎JSON返回它并没有很好地反序列化。
编辑1:添加控制器 编辑2:添加pom.xml 编辑3:
应用目的解决方案后的新错误代码:
Internal Connector Error (1002) - No available client connector supports the required protocol: 'HTTPS'. Please add the JAR of a matching connector to your classpath.
at org.restlet.resource.ClientResource.doError(ClientResource.java:612)
at org.restlet.resource.ClientResource.handleInbound(ClientResource.java:1203)
at org.restlet.resource.ClientResource.handle(ClientResource.java:1027)
at org.restlet.resource.ClientResource.handle(ClientResource.java:969)
at org.restlet.resource.ClientResource.get(ClientResource.java:680)
at aiss.model.resource.KitsuResources.getBusqueda(KitsuResources.java:36)
at aiss.controller.SearchController.doGet(SearchController.java:33)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:128)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:48)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:95)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:508)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
答案 0 :(得分:2)
您的API返回JSON,但您的请求未指定它需要JSON,因此媒体协商失败。
在您声明请求后将其放入:
cr = new ClientResource(URL_Kitsu + searchPath.replace("%QUERY", search));
MediaType custom = MediaType.register("application/vnd.api+json", "application/vnd.api+json");
Engine.getInstance().getRegisteredConverters().add(new JsonConverter()); // In your application, this should be done when start up the app instead of here
cr.accept(custom); // This tell your server you want JSON
busquedaAnime = cr.get(Example.class);
修改强>
经邮递员检查后,我发现API仅生成application/vnd.api+json
个数据。因此,您应将其添加为Accept
标题
编辑2:
添加JSON转换器依赖项(适用于maven)
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.json</artifactId>
<version>2.3.7</version>
</dependency>
编辑3:
为HTTPS添加此内容:
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet</artifactId>
<version>2.3.7</version>
</dependency>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet.ext.simple</artifactId>
<version>2.3.7</version>
</dependency>
在文档中:https://restlet.com/open-source/documentation/user-guide/2.2/core/security/https
除了标准的Restlet jar文件外,您还需要 引用HTTPS的jar文件。 &#39;简单&#39; HTTPS连接器使用这些 jar文件:
lib / org.restlet.ext.simple_3.1.jar \
LIB / org.simpleframework_3.1 / org.simpleframework.jar
lib / org.restlet.ext.ssl.jar \
LIB / org.jsslutils_0.5 / org.jsslutils.jar
编辑4
经过几个小时的挖掘代码后,我发现您正在混合依赖项并且存在太多冲突的库。
.restlet.
群组的所有依赖关系。restlet.version
更改为2.2-M6
(由于包装更改,无法在较新版本中初始化JacksonConverter
。我无法弄清楚,所以请使用旧版本) 添加这些依赖项以使用HTTP和JSON:
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.net</artifactId>
<version>2.2-M6</version>
<exclusions>
<exclusion> // If you use other version that your restlet then you should exclude it, otherwise some strange runtime class conflicting will occur
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet</artifactId>
<version>${restlet.version}</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.jackson</artifactId>
<version>${restlet.version}</version>
</dependency>
因为我们想要处理自定义json格式,但默认的jackson不支持它,所以我们必须定义我们的自定义转换器:
public class CustomJacksonConverter extends JacksonConverter {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@Override
public <T> float score(Representation source, Class<T> target, Resource resource) {
// Restlet will use this function to determine which converter to be use. We yeild 1.0F equals to say: I will handle it from here
if (source.getMediaType().getName().contains("json")) {
return 1.0F;
}
return super.score(source, target, resource);
}
@Override
public <T> T toObject(Representation source, Class<T> target, Resource resource) throws IOException {
// Write stream to object
return OBJECT_MAPPER.readValue(source.getStream(), target);
}
}
注册自定义json转换器和媒体类型
MediaType custom = MediaType.register("application/vnd.api+json", "application/vnd.api+json");
cr.accept(custom);
Engine.getInstance().getRegisteredConverters().add(new CustomJacksonConverter());
享受
busquedaAnime = cr.get(Example.class);
编辑7
使用此库后,我建议您切换到另一个客户端。
我最喜欢的一个是Retrofit
。
它将使您免于HTTPs依赖,媒体协商等的痛苦