2016-02-22 92 views
0

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

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

而且青梅JSP页面一个简单的控制器与简单的形式与POST请求

<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代码(新的或这个控制器)

+0

显示将会的控制器收到'POST'请求。 – acdcjunior

+0

我有一个FORM与控制器。我以这种形式发送POST FORM到url int action。我没有另一个控制器。这个网址重定向到一些页面(不是我的页面),我得到了响应从这个页面到我的服务器控制器(在我需要创建这个新的控制器,那么它是如何做到的?) – user5620472

+0

'

'将'POST'其内容action属性处的URL。在这种形式下,这个URL是'/ getCallDetailGeneral'。因此,您必须在控制器中创建一个@ RequestMapping注释的方法来回答该URL中的POST请求。 – acdcjunior

回答

0

我假设你提交POST请求getCallDetailGeneral并需要读取响应是JSON。 如果你正在使用jQuery,你可以POST到getCallDetailGeneral并阅读json响应。

0

您可以使用The Apache HttpComponentsjsoup库与远程服务器进行交互。

解决方案与阿帕奇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; 
    

解决方案与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(); 
        } 
    } 
    
相关问题