简体   繁体   English

Liferay - 调用远程portlet的控制器

[英]Liferay - Call a controller of a remote portlet

I was trying to call a controller of a portlet belonging to a remote portal. 我试图调用属于远程门户的portlet的控制器。 I tried following this tutorial but it is has a lot of extra stuffs which are causing build errors for me. 我尝试了这个教程,但它有很多额外的东西,这些都会导致我的构建错误。

I have a controller like below in the remote portlet. 我在远程portlet中有一个类似下面的控制器。

@Controller
public class SampleRestController {

    @RequestMapping(value = "/helloSample", method=RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public @ResponseBody String helloSample() {
        return "Finally!";
    }
}

What should I do in order to call the above method using a rest call..? 为了使用休息呼叫调用上述方法,我该怎么办? That is, what are the changes I should make to a basic spring liferay portlet to get the output of http://localhost:port/.../.../helloSample as Finally! 也就是说,我应该对基本的spring liferay portlet进行哪些更改,以获取http://localhost:port/.../.../helloSample的输出为http://localhost:port/.../.../helloSample Finally!

You can have a rest controller working inside a portlet. 您可以在portlet中使用休息控制器。 The linked article Using RESTFul services with Liferay explains it nicely. 链接的文章使用LifeFray服务和Liferay很好地解释了它。 Here's just the summary. 这只是摘要。

Custom servlet inside a portlet portlet中的自定义servlet

What you need to do is to implement a servlet wrapped in a portlet application. 您需要做的是实现一个包装在portlet应用程序中的servlet。 You need to configure Liferay's PortalDelegateServlet in web.xml. 您需要在web.xml中配置Liferay的PortalDelegateServlet The servlet will delegate request processing to Spring's DispatcherServlet . servlet将请求处理委托给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>

The restful servlet needs its separate application context that will contain AnnotationMethodHandlerAdapter , view resolver and JSON mapper. restful servlet需要单独的应用程序上下文,其中包含AnnotationMethodHandlerAdapter ,视图解析器和JSON映射器。

Given the rest controller from your example 给出你的例子中的其余控制器

@Controller
public class SampleRestController {

    @RequestMapping(value = "/helloSample", method=RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public @ResponseBody String helloSample() {
        return "Finally!";
    }
}

The resulting url is composed followingly 产生的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

Sample url for Tomcat deployment: http://localhost:8080/test-1.0-SNAPSHOT/services/helloSample Tomcat部署的示例网址: http:// localhost:8080 / test-1.0-SNAPSHOT / services / helloSample

See the linked article for more details. 有关详细信息,请参阅链接的文章

The original idea for this feature is summarized in Liferay JIRA issue Custom servlets running in the ROOT context . Liferay JIRA问题在ROOT上下文中运行的自定义servlet总结了此功能的最初构思。

Because your controller does not belong to a portlet, you can simply call 因为你的控制器属于一个portlet,你可以简单地调用

http://localhost:port/_context path of your war_/helloSample

As the linked tutorial says: 正如链接教程所说:

So in my sample project I will produce RESTFul services that will be consumed by custom portlets... 所以在我的示例项目中,我将生成将由自定义portlet使用的RESTFul服务......

He did not create a portlet that contains the controller - neither do you. 他没有创建包含控制器的portlet - 你也不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM