我试图调用属于远程门户的 portlet 的 Controller 。 我尝试关注 this教程,但它有很多额外的东西导致我的构建错误。
我在远程 portlet 中有一个如下所示的 Controller 。
@Controller
public class SampleRestController {
@RequestMapping(value = "/helloSample", method=RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody String helloSample() {
return "Finally!";
}
}
我应该怎么做才能使用 rest call 调用上述方法..?也就是说,我应该对基本的 spring liferay portlet 进行哪些更改才能将 http://localhost:port/.../.../helloSample
的输出作为 Finally !
最佳答案
您可以让休息 Controller 在 portlet 中工作。链接文章Using RESTFul services with Liferay很好地解释了它。这只是摘要。
portlet 内的自定义 servlet
您需要做的是实现一个封装在 portlet 应用程序中的 servlet。你需要配置Liferay的PortalDelegateServlet
在 web.xml 中。 servlet 会将请求处理委托(delegate)给 Spring 的 DispatcherServlet
。
<servlet>
<servlet-name>restful</servlet-name>
<servlet-class>com.liferay.portal.kernel.servlet.PortalDelegateServlet</servlet-class>
<init-param>
<param-name>servlet-class</param-name>
<param-value>org.springframework.web.servlet.DispatcherServlet</param-value>
</init-param>
<init-param>
<param-name>sub-context</param-name>
<param-value>restful</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>restful</servlet-name>
<url-pattern>/services/*</url-pattern>
<servlet-mapping>
restful servlet 需要其单独的应用上下文,它将包含 AnnotationMethodHandlerAdapter
、 View 解析器和 JSON 映射器。
给定您示例中的其余 Controller
@Controller
public class SampleRestController {
@RequestMapping(value = "/helloSample", method=RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody String helloSample() {
return "Finally!";
}
}
生成的 url 组成如下
http://host:port/<<context path>>/services/helloSample
| | |
| Context path of your application (eg. test-1.0-SNAPSHOT)
| |
| Defined by servlet mapping in web.xml
|
| Defined by @RequestMapping in the controller
Tomcat 部署示例 url:http://localhost:8080/test-1.0-SNAPSHOT/services/helloSample
参见 the linked article了解更多详情。
此功能的最初想法在 Liferay JIRA 问题 Custom servlets running in the ROOT context 中进行了总结.
关于java - Liferay - 调用远程 portlet 的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33391455/