简体   繁体   English

通过填写表单中的详细信息来进行对其他服务的REST URL调用

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

I have recently started working with Spring MVC framework. 我最近开始使用Spring MVC框架。 I made a lot of progress while reading lots of tutorial on the net. 在网上阅读大量教程的同时,我取得了很大的进步。

Background about my application- 关于我的申请的背景-

I have to make a REST URL call to another service (deployed already on tomcat) by using details provided in the form. 我必须使用表单中提供的详细信息对另一个服务(已在tomcat上部署)进行REST URL调用。 So I have already made a form using JSP whose content is something like this as shown in the picture- I am not sure how can I make a REST url call by making the url from the form entries and then show the response of that url on the next screen. 因此,我已经使用JSP制作了一个表单,其内容如下图所示:我不确定如何通过表单条目中的URL来进行REST URL调用,然后显示该URL的响应。下一个屏幕。

So in the above form if I have written User Id as 1000012848 , and checkbox is selected (means true) for Debug Flag and in the Attribute Name I have selected first row ( in general we can select all three as well) and Machine Name is localhost and Port Number is 8080 then url should look something like this- 因此,在上面的表格中,如果我将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

So in all our URL that we will be making from the form entries, the below line will always be there at the same place- and then after that each form entry will start getting appended 因此,在我们要从表单条目创建的所有URL中,下一行将始终位于同一位置-然后,每个表单条目将开始被附加

service/newservice/v1/get/

Now after making the above url, as soon as I will be clicking submit, it will be making a call to the above url and whatever response it gets from the URL, it will show in the next screen (result.jsp file) which I am not sure how to do that? 现在,在创建完上面的URL之后,我将单击“提交”,它将立即调用上面的URL,并且无论从URL得到什么响应,它将显示在下一个屏幕(result.jsp文件)中,不知道该怎么做? Below are my files which I have created. 以下是我创建的文件。 Can anyone help me out in solving my problem? 谁能帮我解决我的问题? What code changes I will be needing to do this problem? 为了解决这个问题,我需要进行哪些代码更改?

student.jsp file (which makes the form) 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 file (which I am going to use to show the result after hitting that url) 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 Class- 控制器类

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

You can to use RestTemplate for calling RESTful URLs from your spring component 您可以使用RestTemplate从spring组件中调用RESTful URL

So, Your controller method can be as below 因此,您的控制器方法可以如下

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

}

Your spring configuration should register the restTemplate as below: 您的spring配置应该注册restTemplate,如下所示:

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

See RestTemplate doc for more details. 有关更多详细信息,请参见RestTemplate文档

The above should do. 以上应该做。

One suggestion.. Your RESTful URL ( http://localhost:8080/service/newservice/v1/get/PP.USERID=1000012848, debugflag=true/host.profile.ACCOUNT ) is really terrible. 一个建议..您的RESTful URL( http://localhost:8080/service/newservice/v1/get/PP.USERID=1000012848, debugflag=true/host.profile.ACCOUNT )确实很糟糕。 Once you resolve your problem, I recommend you to google for how a good RESTful URL shuld look like. 解决问题后,建议您去google一下一个好的RESTful URL。

Cheers, Vinay Vinay干杯

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

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