2013-01-21 28 views
2

來調用另一個服務的REST URL我最近開始使用Spring MVC框架。在網上閱讀大量教程的同時,我取得了很多進展。通過填寫表格的細節

背景有關我的應用程序 -

我必須做出一個REST URL呼叫使用的形式提供詳細情況的另一服務(Tomcat上已經部署)。所以我已經使用了JSP的形式,其內容如圖所示 - 我不確定如何通過從表單條目創建URL並顯示該URL的響應來實現REST URL調用下一個屏幕。

所以,如果我寫User Id as 1000012848,並checkbox is selected (means true) for Debug Flag和上述形式的Attribute Name I have selected first row(一般我們可以選擇三個爲好)和Machine Name is localhostPort Number is 8080然後URL看起來應該像這個 -

http://localhost:8080/service/newservice/v1/get/PP.USERID=1000012848,debugflag=true/host.profile.ACCOUNT 

因此,在我們所有的URL,我們將從表單條目來製作,下面的線將永遠存在在同一地方 - 然後經過每個表單條目將開始得到附加

service/newservice/v1/get/ 

現在,在創建上面的url之後,只要我點擊submit,它就會調用上面的url,並從URL獲得任何響應,它會在下一個屏幕(result.jsp文件)中顯示哪個我不知道該怎麼做?以下是我創建的文件。任何人都可以幫我解決我的問題嗎?我將需要做什麼代碼更改來解決這個問題?

student.jsp文件(這使得形式)

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<res:head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <meta name="layout" content="main" /> 
    <title>First Tutorial</title> 
</res:head> 
<res:body> 

    <form:form method="POST" action="/_hostnewapp/addStudent"> 
     <table> 
      <tr> 
       <td><form:label path="userId">User Id</form:label></td> 
       <td><form:input path="userId" /></td> 
      </tr> 
      <tr> 
       <td>Debug Flag :</td> 
       <td><form:checkbox path="debugFlag" /></td> 
      </tr> 
      <tr> 
       <td>Attribute Name</td> 
       <td><form:select path="attributeNames" items="${attributeNamesList}" 
         multiple="true" /></td> 
      </tr> 
<!--  <tr> 
       <td>Environment</td> 
       <td><form:checkboxes items="${environmentList}" 
         path="environments" /></td> 
      </tr> 
-->    
      <tr> 
       <td><form:label path="machineName">Machine Name</form:label></td> 
       <td><form:input path="machineName" /></td> 
      </tr> 
      <tr> 
       <td><form:label path="portNumber">Port Number</form:label></td> 
       <td><form:input path="portNumber" /></td> 
      </tr> 

      <tr> 
       <td colspan="2"><input type="submit" value="Submit" /></td> 
      </tr> 
     </table> 
    </form:form> 

</res:body> 
</html> 

result.jsp中文件(我將使用擊中該網址後顯示結果)

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<res:head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <meta name="layout" content="main" /> 
    <title>HostDomain</title> 
</res:head> 
<res:body> 

    <h2>Response after submitting the result</h2> 
    // Not sure what I need to add here to show the result after hitting the url 

</res:body> 
</html> 

控制器類 -

@Controller 
public class SampleRaptorController { 

    @RequestMapping(value = "/student", method = RequestMethod.GET) 
    public ModelAndView student() { 
     return new ModelAndView("student", "command", new Student()); 
    } 

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST) 
    public String addStudent(@ModelAttribute("SpringWeb") Student student, 
      ModelMap model) { 
     model.addAttribute("userId", student.getUserId()); 

     return "result"; 
    } 


    @ModelAttribute("attributeNamesList") 
    public Map<String,String> populateSkillList() { 

     //Data referencing for java skills list box 
     Map<String,String> attributeNamesList = new LinkedHashMap<String,String>(); 
     attributeNamesList.put("ACCOUNT","host.profile.ACC"); 
     attributeNamesList.put("ADVERTISING","host.profile.ADV"); 
     attributeNamesList.put("SEGMENTATION","host.profile.SEG"); 

     return attributeNamesList; 
    } 
} 

回答

5

您可以使用RestTemplate從您的spring co調用RESTful URL mponent

所以,你的控制器方法可以如下

@Controller 
public class SampleRaptorController { 

    @Autowired 
    RestTemplate restTemplate; 

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST) 
    public String addStudent(@ModelAttribute("SpringWeb") Student student, 
        Model model){ 

     // Build URL 
     StringBuilder url = new StringBuilder(). 
         append("http://localhost:8080/service/newservice/v1/get"). 
         append("?PP.USERID=" + student.getUserId). 
         append("&debugflag=" + student.isDebugFlag);// so on 

     // Call service 
     String result = restTemplate.getForObject(url.toString(), String.class); 
     model.addAttribute("result", result); 

     return "result"; 
    } 

} 

Spring配置應該註冊restTemplate如下:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/> 

詳情請參閱RestTemplate doc

以上應該做的。

一個建議..您的REST風格的URL(http://localhost:8080/service/newservice/v1/get/PP.USERID=1000012848, debugflag=true/host.profile.ACCOUNT)真的很糟糕。一旦你解決了你的問題,我建議你谷歌如何一個良好的RESTful URL shuld看起來像。

乾杯, 維奈

+0

我想這一個,但我在'@Autowired RestTemplate restTemplate有例外;'這樣的事情'產生的原因:org.springframework.beans。factory.BeanCreationException:無法自動裝入字段:org.springframework.web.client.RestTemplate com.ebay.app.raptor.controller.SampleRaptorController.restTemplate;'任何想法爲什麼? – ferhan

+1

對不起。 restTemplate需要在Spring上下文中註冊。更新了答案 – Vinay