Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 799
Anthony Avatar asked Apr 15 '26 14:04

Anthony


1 Answers

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

like image 163
helderdarocha Avatar answered Apr 18 '26 04:04

helderdarocha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!