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

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

问题描述

有3个问题。我正在使用Java Restful webservices并且请求是HTTP POST

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


  1. 客户端如何发送JSON数据以及applicationType / x-的MediaType WWW的形式,进行了urlencoded。
    是否可以使用字节数组?

  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数据以及应用程序的MediaType / x-www-form-urlencoded?

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)邮递员会自动对JSON进行网址编码。只需输入一个键,值就是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-endoded 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} 进入值,它运行正常。响应是世界

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

我也用邮差测试了这个,它运行正常。我用过这段代码

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 作为键。根据上述方法的请求,我得到相同的世界结果。

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.

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

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