POST请求后如何从响应中获取参数?

时间:2016-02-22 04:13:59

标签: java spring-mvc

我有一个简单的控制器,例如

    @Controller
    @RequestMapping("/")
    public class HelloController {
        @RequestMapping(method = RequestMethod.GET)
        public String printWelcome(ModelMap model) throws NoSuchAlgorithmException {
...
     model.addAttribute("startDate", startDate);
     model.addAttribute("endDate", endDate);
     model.addAttribute("msisdn", msisdn);
            return "hello";
        }

带有POST请求的简单表单的ome JSP页面

<form action="http://1.....:8887/getCallDetailGeneral" method="post" enctype="multipart/form-data">
    <input type="hidden" name="startDate" value="${startDate}"/>
    <input type="hidden" name="endDate" value="${endDate}"/>
    <input type="hidden" name="msisdn" value="${msisdn}"/>
    <input type="submit" value="send">
</form>

当我按下send按钮时,我发送POST请求并得到一些回复。

我不明白如何从此响应中获取参数?或我的代码错误。

例如,这个响应返回给我JSON字符串。我怎样才能将它传递给我的java代码(新的或这个控制器)

2 个答案:

答案 0 :(得分:0)

我假设您向getCallDetailGeneral提交POST请求,并且需要读取JSON的响应。 如果您使用的是jquery,则可以POST到getCallDetailGeneral并读取json响应。

答案 1 :(得分:0)

您可以使用 enter image description here The Apache HttpComponents 库与远程服务器进行交互。

使用 Apache HttpComponents 库解决方案:

  1. 依赖 添加到pom.xml

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.1</version>
    </dependency>
    
  2. 表格:

    <form action="/" method="post">
        <input type="text" name="startDate"/>
        <input type="text" name="endDate"/>
        <input type="text" name="msisdn"/>
        <input type="submit"/>
    </form>
    
  3. 通过GET方法制作控制器 展示表单

    @RequestMapping(method = RequestMethod.GET)
    public String indexController(){
        return "index";
    }
    
  4. 通过Rest.java方法从表单中创建控制器(POST)而不是 接收参数 传递它们到远程服务器

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    @RestController
    public class Rest {
        @RequestMapping(method = RequestMethod.POST)
        public String getData(@RequestParam(value="startDate") String startDate,
                              @RequestParam(value="endDate") String endDate,
                              @RequestParam(value="msisdn") String msisdn) throws IOException {
    
            String result = "";
            CloseableHttpClient httpclient = HttpClients.createDefault();
            try {
                HttpPost httpPost = new HttpPost("http://localhost:8080");
    
                List<NameValuePair> urlParameters = new ArrayList<>();
                urlParameters.add(new BasicNameValuePair("startDate", startDate));
                urlParameters.add(new BasicNameValuePair("endDate", endDate));
                urlParameters.add(new BasicNameValuePair("msisdn", msisdn));
                HttpEntity postParams = new UrlEncodedFormEntity(urlParameters);
                httpPost.setEntity(postParams);
    
                result += "Executing request " + httpPost.getRequestLine() + "\n";
    
                //Create a custom response handler
                ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
    
                    @Override
                    public String handleResponse(
                            final HttpResponse response) throws ClientProtocolException, IOException {
                        int status = response.getStatusLine().getStatusCode();
                        if (status >= 200 && status < 300) {
                            HttpEntity entity = response.getEntity();
                            return entity != null ? EntityUtils.toString(entity) : null;
                        } else {
                            throw new ClientProtocolException("Unexpected response status: " + status);
                        }
                    }
    
                };
                String responseBody = httpclient.execute(httpPost, responseHandler);
                result += responseBody;
            } finally {
                httpclient.close();
            }
    
            return result;
        }
    }
    
  5. http://localhost:8080替换为您的远程服务器地址;

  6. 您可以使用以下代码从远程服务器获取响应:

    String responseBody = httpclient.execute(httpPost, responseHandler);
    result += responseBody;
    
  7. jsoup 库的解决方案:

    1. 依赖 添加到pom.xml

      <dependency>
          <groupId>org.jsoup</groupId>
          <artifactId>jsoup</artifactId>
          <version>1.8.3</version>
      </dependency>
      
    2. 通过Rest.java方法从表单中创建控制器(POST)而不是 接收参数 传递它们到远程服务器

      import org.jsoup.Jsoup;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      import org.springframework.web.bind.annotation.RequestParam;
      import org.springframework.web.bind.annotation.RestController;
      
      import java.io.IOException;
      
      @RestController
      public class Rest {
          @RequestMapping(method = RequestMethod.POST)
          public String getData(@RequestParam(value="startDate") String startDate,
                                @RequestParam(value="endDate") String endDate,
                                @RequestParam(value="msisdn") String msisdn) throws IOException {
      
              return Jsoup.connect("http://localhost:8080")
                      .data("startDate", startDate)
                      .data("endDate", endDate)
                      .data("msisdn", msisdn)
                      .post()
                      .toString();
          }
      }