通过填写表单中的详细信息,对另一个服务进行REST URL调用 [英] Make a REST URL call to another service by filling the details from the form
问题描述
我最近开始使用Spring MVC框架。我在网上阅读大量教程时取得了很多进展。
I have recently started working with Spring MVC framework. I made a lot of progress while reading lots of tutorial on the net.
关于我的申请的背景 -
我必须使用表单中提供的详细信息对其他服务(已在tomcat上部署)进行REST URL调用。所以我已经使用JSP创建了一个表单,其内容如图所示 - 我不知道如何通过从表单条目中创建url然后显示该URL的响应来进行REST URL调用下一个屏幕。
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.
所以在上面的表格中如果我写了用户ID为1000012848
,并且复选框被选中(表示为真),用于Debug Flag
,在属性名称中我选择了第一行
(通常我们可以选择全部三个)和机器名是localhost
和端口号是8080
然后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-
http://localhost:8080/service/newservice/v1/get/PP.USERID=1000012848,debugflag=true/host.profile.ACCOUNT
因此,在我们将从表单条目中创建的所有URL中,以下行将永远在同一个地方 - 然后每个表单条目将开始附加
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/
现在制作完上面的网址后,只要我点击提交,它就会调用上面的网址,无论网页得到什么回复,它都会显示在下一个屏幕(result.jsp文件)中我不知道该怎么办?以下是我创建的文件。任何人都可以帮助我解决我的问题吗?我将需要做哪些代码更改?
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文件(生成表单)
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文件(我将用它来点击该网址后显示结果)
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
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
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";
}
}
你的弹簧配置应该注册restTemplate如下所示:
Your spring configuration should register the restTemplate as below:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>
参见 RestTemplate doc 了解更多详情。
以上应该做。
一个建议..您的RESTful URL( http:// localhost:8080 / service / newservice / v1 / get / PP.USERID = 1000012848 ,debugflag = true / host.profile.ACCOUNT
)非常可怕。一旦您解决了问题,我建议您谷歌搜索一个好的RESTful URL的样子。
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.
干杯,
Vinay
Cheers, Vinay
这篇关于通过填写表单中的详细信息,对另一个服务进行REST URL调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!