解析媒体类型'application / json; encoding = utf8,charset = utf-8'时出错

时间:2022-06-10 19:33:18

I'm using DropWizard with jersey to make a client that accepts JSON from a server and maps it to a POJO. However, I get this error when invoking the client.

我正在使用DropWizard和jersey来创建一个从服务器接受JSON并将其映射到POJO的客户端。但是,我在调用客户端时收到此错误。

java.lang.IllegalArgumentException: Error parsing media type 'application/json;encoding=utf8, charset=utf-8'

My code is as follows:

我的代码如下:

@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;
  }
}

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


4  

Are you generating that header in your client?

你在客户端生成标题吗?

According to W3C - 4 The Content-Type Header Field the Content-type header must have the format:

根据W3C - 4 Content-Type Header字段,Content-type标头必须具有以下格式:

Content-Type := type "/" subtype *[";" parameter] 

where

哪里

parameter := attribute "=" value

So you could have as a parseable media type:

所以你可以拥有一个可解析的媒体类型:

Content-type: application/json; encoding=utf8; charset=utf8

Using a semicolon instead of a comma. This doesn't mean it's correct, just parseable. Try using instead:

使用分号而不是逗号。这并不意味着它是正确的,只是可解析的。请尝试使用:

Content-type: application/json; charset=utf8

If that's a client error, then you might need to configure an Accept header (and the Content-type is not necessary since you aren't sending any payload). Try it without specifying the charset (if it fails, it will fail for another reason). You can test different headers and encodings using an interactive REST client, such as the Firefox REST client

如果这是客户端错误,那么您可能需要配置Accept标头(并且由于您没有发送任何有效负载,因此不需要Content-type)。尝试它而不指定字符集(如果失败,它将因其他原因而失败)。您可以使用交互式REST客户端(例如Firefox REST客户端)测试不同的标头和编码

#1


4  

Are you generating that header in your client?

你在客户端生成标题吗?

According to W3C - 4 The Content-Type Header Field the Content-type header must have the format:

根据W3C - 4 Content-Type Header字段,Content-type标头必须具有以下格式:

Content-Type := type "/" subtype *[";" parameter] 

where

哪里

parameter := attribute "=" value

So you could have as a parseable media type:

所以你可以拥有一个可解析的媒体类型:

Content-type: application/json; encoding=utf8; charset=utf8

Using a semicolon instead of a comma. This doesn't mean it's correct, just parseable. Try using instead:

使用分号而不是逗号。这并不意味着它是正确的,只是可解析的。请尝试使用:

Content-type: application/json; charset=utf8

If that's a client error, then you might need to configure an Accept header (and the Content-type is not necessary since you aren't sending any payload). Try it without specifying the charset (if it fails, it will fail for another reason). You can test different headers and encodings using an interactive REST client, such as the Firefox REST client

如果这是客户端错误,那么您可能需要配置Accept标头(并且由于您没有发送任何有效负载,因此不需要Content-type)。尝试它而不指定字符集(如果失败,它将因其他原因而失败)。您可以使用交互式REST客户端(例如Firefox REST客户端)测试不同的标头和编码