掲示板

Using Spring to map a RESTful webservice call onto a controller

11年前 に Adam Hardy によって更新されました。

Using Spring to map a RESTful webservice call onto a controller

New Member 投稿: 19 参加年月日: 12/05/18 最新の投稿
Hi,

I have some functionality in a portlet controller that I would also like to expose as a RESTful webservice.

I wanted to use the @RequestMapping annotation but first, I'm not sure whether I got the code right because I'm getting a NullPointerException from the Liferay PortletServlet.


@Controller("forgottenDetailsController")
@RequestMapping(value = "VIEW")
@SessionAttributes(value = {"forgottenDetailsForm", "noOfQuestions"})
public class ForgottenDetailsController extends WebsiteController
{



other methods for normal portlet stuff.... then my attempt at a RESTful webservice:


    @RequestMapping(
        value = "forgottenDetails/{instCode}/{loginId}/{newPassword}/{sendLogin}",
        method = RequestMethod.POST)
    public HttpStatus resetPassword(@PathVariable String instCode, 
        @PathVariable Long loginId, @PathVariable boolean newPassword, 
        @PathVariable boolean sendLogin) 
    {
        try 
        {
            return HttpStatus.OK;
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            return HttpStatus.METHOD_FAILURE;
        }   
    }


I'm trying posting to http://localhost:8080/web/forgottenDetails/PPP/1234/true/false

I have a bad feeling that I'm trying to hammer in screws here but I've been googling all morning for a solution without getting anywhere.

Can anyone help?

Thanks!
11年前 に Adam Hardy によって更新されました。

RE: Using Spring to map a RESTful webservice call onto a controller

New Member 投稿: 19 参加年月日: 12/05/18 最新の投稿
Just to update this for the sake of completion, I was able to use a portlet controller method to serve a webservice using the resource mapping, however I was unable to apply the handy Spring REST framework to allow serialisation and deserialisation of objects or URL parameter annotations in the controller method signature. Whether that had anything to do with Liferay, I'm not sure.
11年前 に Nayden Nikolov によって更新されました。

RE: Using Spring to map a RESTful webservice call onto a controller

New Member 投稿: 4 参加年月日: 11/04/28 最新の投稿
Hi,
what exactly you use in the end to finish it.
You create a own web service or you use Spring REST Framework.
10年前 に Pavan Vadlamudi によって更新されました。

RE: Using Spring to map a RESTful webservice call onto a controller

New Member 投稿: 1 参加年月日: 13/06/25 最新の投稿
Adam,

I know its been a while, but just checking if you did figure out the solution and if so, can you please share your approach.

Thanks.
Pavan.
7年前 に Krzysztof Jastrzębski によって更新されました。

RE: Using Spring to map a RESTful webservice call onto a controller

New Member 投稿: 2 参加年月日: 16/12/05 最新の投稿
Sorry for refreshing so old topic but I didn't want to make new one.

Maybe someone found a solution for this problem?

I had a similar problem. Created Class in com.test.controller.rest package with annotation @RestController with mappings to methods (@RequestMapping).
In web.xml added:

    <servlet>
        <servlet-name>restful</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>restful</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>


and created restful-servlet.xml with:


<!--?xml version="1.0" encoding="UTF-8"?-->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.test.controller.rest" />

</beans>


My app name is test-app and when I try to access http://localhost:8080/test-app/services/get-test i get 404 error.

What should I do to get those services working?
7年前 に Krzysztof Jastrzębski によって更新されました。

RE: Using Spring to map a RESTful webservice call onto a controller

New Member 投稿: 2 参加年月日: 16/12/05 最新の投稿
I think I have found reason of this problem.
Liferay 7 do not deploy app to webapps but make app as osgi plugin (I do not know about that anything so I can be wrong). I don't know why, but in this situation I cannot use servlet defined in web.xml. After I unpack .war to webapps folder, my servlets started working.

Do you have some clue how it should be done properly in Liferay 7 (custom servlets deployment) or how to turn off this osgi deploying to have standard deploying to webapps folder (like in Liferay 6)?
thumbnail
7年前 に Milen Dyankov によって更新されました。

RE: Using Spring to map a RESTful webservice call onto a controller

Expert 投稿: 310 参加年月日: 12/10/30 最新の投稿
It's really better to create a new thread and explain your problem there (especially if you use much different Liferay version) than to resurrect ages old threads!

I assume you have a WAR file (since you have web.xml) that you deploy. In that case in Liferay 7 it's indeed converted to bundle and deployed in OSGi run time. Normally the web resources in it should be available under the "/o"context. So in your case it should be
http://localhost:8080/o/test-app/services/get-test 


However if you only want to create REST service there is much easier way. See https://github.com/liferay/liferay-blade-samples#rest-service
11年前 に Holger García Fernández によって更新されました。

RE: Using Spring to map a RESTful webservice call onto a controller

New Member 投稿: 1 参加年月日: 12/10/28 最新の投稿
Could you please post your solution to this problem, I am struggling with this and haven´t find a suitable solution by myself.