我将 DropWizard 与 jersey 一起使用来制作一个客户端,该客户端从服务器接受 JSON 并将其映射到 POJO。但是,调用客户端时出现此错误。
java.lang.IllegalArgumentException: Error parsing media type 'application/json;encoding=utf8, charset=utf-8'
我的代码如下:
@Path("/something")
@Produces(MediaType.APPLICATION_JSON)
public class SampleClient {
final Client client;
WebResource.Builder builder;
public SampleClient (Client client) {
this.client = client;
this.builder = client.resource("http://localhost/mysample/service").type("application/json");
}
@GET
public MyMapper getSomething() {
MyMapper result = builder.accept("application/json").get(MyMapper.class);
return result;
}
}
我做错了什么?
最佳答案
您是否在您的客户端中生成该 header ?
根据 W3C - 4 The Content-Type Header Field Content-type
header 必须具有以下格式:
Content-Type := type "/" subtype *[";" parameter]
在哪里
parameter := attribute "=" value
所以你可以有一个可解析的媒体类型:
Content-type: application/json; encoding=utf8; charset=utf8
使用分号代替逗号。这并不意味着它是正确的,只是可解析的。尝试改用:
Content-type: application/json; charset=utf8
如果这是一个客户端错误,那么您可能需要配置一个 Accept
header (并且 Content-type 不是必需的,因为您没有发送任何负载)。在不指定字符集的情况下尝试(如果失败,则会因其他原因而失败)。您可以使用交互式 REST 客户端测试不同的 header 和编码,例如 Firefox REST client
关于java - 解析媒体类型时出错 'application/json;encoding=utf8, charset=utf-8',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22185776/