java - Liferay - 调用远程 portlet 的 Controller

标签 java spring liferay

我试图调用属于远程门户的 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/

相关文章:

java - 导航回 FragmentPagerAdapter,listFragment 为空

java - 说话失败未绑定(bind)到 TTS 引擎

java - ClassNotFoundException : org. hibernate.ejb.HibernatePersistence 使用 maven 部署

java - 如何以编程方式将内容写入json文件

java - java中的IllegalMonitorStateException

java - Spring Boot 集成测试响应空体 - MockMvc

java - T-SQL 存储过程不会通过 COUNT(*) 在空 SELECT 结果上返回零

tomcat - 删除 tomcat 中的临时文件夹和工作文件夹会损坏我的安装

liferay - java.sql.SQLException : Connections could not be acquired from the underlying database! — HSQLDB

java - 如何明智地构建Maven项目包?