java 通过填写表单中的详细信息,对另一个服务进行 REST URL 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14432167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make a REST URL call to another service by filling the details from the form
提问by arsenal
I have recently started working with Spring MVC framework. I made a lot of progress while reading lots of tutorial on the net.
我最近开始使用 Spring MVC 框架。我在网上阅读了大量教程的同时取得了很大进步。
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.
我必须使用表单中提供的详细信息对另一个服务(已部署在 tomcat 上)进行 REST URL 调用。所以我已经使用 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
,并checkbox is selected (means true) for Debug Flag
与在Attribute Name I have selected first row
(一般我们可以选择三个为好),并Machine Name is localhost
和Port 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? 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?
现在,在创建上述 url 后,只要我点击提交,它就会调用上述 url 以及它从该 URL 获得的任何响应,它将显示在下一个屏幕(result.jsp 文件)中,我不知道该怎么做?以下是我创建的文件。谁能帮我解决我的问题?我需要更改哪些代码才能解决此问题?
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;
}
}
回答by Vinay
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 docfor 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. Once you resolve your problem, I recommend you to google for how a good RESTful URL shuld look like.
一个建议.. 你的 RESTful URL ( http://localhost:8080/service/newservice/v1/get/PP.USERID=1000012848, debugflag=true/host.profile.ACCOUNT
) 真的很糟糕。一旦你解决了你的问题,我建议你去谷歌搜索一个好的 RESTful URL 应该是什么样子。
Cheers, Vinay
干杯,维奈