简体   繁体   English

将复杂的参数POST到REST服务,请求URL和正文的内容将是什么样

[英]POST complex parameters to REST service what would request URL and Body look like

So I am standing up a rest service. 所以我要站起来休息。 It is going to request a post to a resource that is pretty complex. 它将请求发布到非常复杂的资源。 The actual backend requires multiple (19!) parameters. 实际的后端需要多个(19!)参数。 One of which is a byte array. 其中之一是字节数组。 It sounds like this is going to need to be serialized by the client and sent to my service. 听起来这需要客户端进行序列化并将其发送到我的服务。

I am trying to understand how I can right a method that will handle this. 我试图了解如何正确处理此问题的方法。 I am thinking something like this 我在想这样的事情

@POST
@Path("apath")
@Consumes(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML)
public Response randomAPI(@Parameter apiID, WhatParamWouldIPutHere confused){
}

What parameter types would I do to capture that inbound (Serialized) Post data. 我将执行什么参数类型来捕获该入站(序列化)的Post数据。 And what would the client URI look like? 客户端URI是什么样的?

In order to get all the parameters of the request you can use @Context UriInfo as parameter of your randomAPI method. 为了获取请求的所有参数,您可以使用@Context UriInfo作为您的randomAPI方法的参数。

Then use UriInfo#getQueryParameters() to get the full MultivaluedMap of parameters. 然后使用UriInfo#getQueryParameters()得到充分的MultivaluedMap的参数。

if you wish to convert the MultivaluedMap to a simple HashMap I added the code for that too. 如果您希望将MultivaluedMap转换为简单的HashMap我也为此添加了代码。

so your method will basically look like something like this: 因此您的方法基本上看起来像这样:

@POST
@Path("apath")
@Consumes(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML)
public Response randomAPI(@Context UriInfo uriInfo){
    Map params= (HashMap) convertMultiToHashMap(uriInfo.getQueryParameters());
    return service.doWork(params);
}

public Map<String, String> convertMultiToHashMap(MultivaluedMap<String, String> m) {
        Map<String, String> map = new HashMap<String, String>();
        for (Map.Entry<String, List<String>> entry : m.entrySet()) {
            StringBuilder sb = new StringBuilder();
            for (String s : entry.getValue()) {
                sb.append(s);
            }
            map.put(entry.getKey(), sb.toString());
        }
        return map;
    }

Additional Info : 附加信息 :

The @Context annotation allows you to inject instances of javax.ws.rs.core.HttpHeaders , javax.ws.rs.core.UriInfo , javax.ws.rs.core.Request , javax.servlet.HttpServletRequest , javax.servlet.HttpServletResponse , javax.servlet.ServletConfig , javax.servlet.ServletContext , and javax.ws.rs.core.SecurityContext objects. @Context批注允许您注入javax.ws.rs.core.HttpHeadersjavax.ws.rs.core.UriInfojavax.ws.rs.core.Requestjavax.servlet.HttpServletRequestjavax.servlet.HttpServletResponse实例javax.servlet.HttpServletResponsejavax.servlet.ServletConfigjavax.servlet.ServletContextjavax.ws.rs.core.SecurityContext对象。

What I am thinking is you can simply use the httpservlet request and all the parameters can be retrieved as below 我在想的是您可以简单地使用httpservlet请求,并且可以按以下方式检索所有参数

@RequestMapping(value = "/yourMapping", method = RequestMethod.POST)
public @ResponseBody String yourMethod(HttpServletRequest request) {
          Map<String, String[]> params = request.getParameterMap();
          //Loop through the parameter maps and get all the paramters one by one including byte array
          for(String key : params){
            if(key == "file"){  //This param is byte array/ file data, specially handle it
            byte[] content = params.get(key);
             //Do what ever you want with the byte array

            else if(key == "any of your special params") {
             //handle
            }

            else {
            }

          }
}

http://docs.oracle.com/cd/E17802_01/products/products/servlet/2.3/javadoc/javax/servlet/ServletRequest.html#getParameterMap%28%29 http://docs.oracle.com/cd/E17802_01/products/products/servlet/2.3/javadoc/javax/servlet/ServletRequest.html#getParameterMap%28%29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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