arsenal
arsenal

Reputation: 24174

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

I have recently started working with Spring MVC framework. 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. 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.

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-

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

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? 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)

<%@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)

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

Upvotes: 2

Views: 17243

Answers (1)

Vinay
Vinay

Reputation: 2817

You can to use RestTemplate for calling RESTful URLs from your spring component

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:

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

See RestTemplate doc for more details.

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. Once you resolve your problem, I recommend you to google for how a good RESTful URL shuld look like.

Cheers, Vinay

Upvotes: 5

Related Questions