簡體   English   中英

通過填寫表單中的詳細信息來進行對其他服務的REST URL調用

[英]Make a REST URL call to another service by filling the details from the form

我最近開始使用Spring MVC框架。 在網上閱讀大量教程的同時,我取得了很大的進步。

關於我的申請的背景-

我必須使用表單中提供的詳細信息對另一個服務(已在tomcat上部署)進行REST URL調用。 因此,我已經使用JSP制作了一個表單,其內容如下圖所示:我不確定如何通過表單條目中的URL來進行REST URL調用,然后顯示該URL的響應。下一個屏幕。

因此,在上面的表格中,如果我將User Id as 1000012848編寫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之后,我將單擊“提交”,它將立即調用上面的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文件(我將使用該文件顯示該URL后顯示結果)

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

您可以使用RestTemplate從spring組件中調用RESTful URL

因此,您的控制器方法可以如下

@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文檔

以上應該做。

一個建議..您的RESTful URL( http://localhost:8080/service/newservice/v1/get/PP.USERID=1000012848, debugflag=true/host.profile.ACCOUNT )確實很糟糕。 解決問題后,建議您去google一下一個好的RESTful URL。

Vinay干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM