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

痴心易碎 提交于 2019-12-06 05:06:19

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!