在 webservice 中使用 JSON 字节数组以及 application/x-www-form-urlencoded [英] Consuming JSON byte array along with application/x-www-form-urlencoded in webservice

查看:60
本文介绍了在 webservice 中使用 JSON 字节数组以及 application/x-www-form-urlencoded的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有 3 个问题.我正在使用 Java Restful 网络服务,请求是 HTTP POST

There are 3 questions. I am using Java Restful webservices and request is HTTP POST

  1. 客户端如何将 JSON 数据与 application/x-www-form-urlencoded 的 MediaType 一起发送.是否有可能使用字节数组?

  1. how can client send JSON data along with MediaType of application/x-www-form-urlencoded. Is it somehow possible using byte array ?

如何使用在服务器端以 byte[] 格式发送的 JSON 数据以及 application/x-www-form-urlencoded 的 MediaType?

How can I consume the JSON data which is sent in byte[] format at server side along with MediaType of application/x-www-form-urlencoded?

我可以使用 Postman 客户端发送字节数组格式的 JSON 吗?

Can I send JSON in byte array format using Postman client ?

推荐答案

  1. (1) Postman 会自动对 JSON 进行 url 编码.只需输入一个键和值就是 JSON.(2) 是的,但您需要先对字节数组进行 Base64 编码.请参阅 Java 8 的 <代码>Base64.Encoder.encodeToString.如果您不使用 Java 8,您可能希望获得一个外部库.

  1. (1) Postman will automatically url-encode the JSON. Just enter a key and value is the JSON. (2) Yes, but you will want to Base64 encode the byte array first. See Java 8's Base64.Encoder.encodeToString. If you're not using Java 8, you'll probably want to get an external library.

(1) 如果你只是发送 url-endded JSON,并且你想 POJOify JSON,那么你应该使用像 Jackson 这样的库.你可以做类似的事情

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

我用 Postman 对此进行了测试,在键中输入 json,在值中输入 {"hello":"world"},效果很好.响应是 world

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) 如果你要对它进行 Base64 编码,那么你需要做类似的事情

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

我也用 Postman 对此进行了测试,效果很好.我用了这段代码

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());

要获取编码字符串(即 eyJoZWxsbyI6IndvcmxkIn0=),请将其作为值并将 base64 作为键.通过对上述方法的请求,我得到相同的 world 结果.

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.

我认为这应该在上面介绍.

I think this should be covered above.

这篇关于在 webservice 中使用 JSON 字节数组以及 application/x-www-form-urlencoded的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆