(1) If you just send the url-endoded JSON, and you want to POJOify the JSON, then you should work with a library like Jackson. You could do something like
@Path("/encoded")
public class EncodedResource {
@POST
@Path("/json")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response getResponse(@FormParam("json") String json)
throws Exception {
ObjectMapper mapper = new ObjectMapper();
Hello hello = mapper.readValue(json, Hello.class);
return Response.ok(hello.hello).build();
}
public static class Hello {
public String hello;
}
}
I've tested this with Postman, typing json
into the key and {"hello":"world"}
into the value, and it works fine. The reponse is world
(2) If you are going to Base64 encode it, then you need to do something like
@POST
@Path("/base64")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response getResponse(@FormParam("base64") String base64)
throws Exception {
String decoded = new String(Base64.getDecoder().decode(base64));
ObjectMapper mapper = new ObjectMapper();
Hello hello = mapper.readValue(decoded, Hello.class);
return Response.ok(hello.hello).build();
}
public static class Hello {
public String hello;
}
I've tested this also with Postman, and it works fine. I used this code
String json = "{\"hello\":\"world\"}";
String encoded = Base64.getEncoder().encodeToString(json.getBytes());
to get an encode string (which is eyJoZWxsbyI6IndvcmxkIn0=
), put that as the value and base64
as the key. With a request to the method above, I get the same world
result.