简体   繁体   中英

Error parsing media type 'application/json;encoding=utf8, charset=utf-8'

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.

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?

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:

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

Basically you need to remove the comma "," in the Content-type string

Or, you need to wrap the comma in double quotes

example

Content-type: video/mp4; codecs="avc1.64001F,mp4a.40.2"

the "comma" in "codecs" has to be wrapped in double-quotes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM