Foren

Using Spring to map a RESTful webservice call onto a controller

Adam Hardy, geändert vor 11 Jahren.

Using Spring to map a RESTful webservice call onto a controller

New Member Beiträge: 19 Beitrittsdatum: 18.05.12 Neueste Beiträge
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!
Adam Hardy, geändert vor 11 Jahren.

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

New Member Beiträge: 19 Beitrittsdatum: 18.05.12 Neueste Beiträge
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.
Nayden Nikolov, geändert vor 11 Jahren.

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

New Member Beiträge: 4 Beitrittsdatum: 28.04.11 Neueste Beiträge
Hi,
what exactly you use in the end to finish it.
You create a own web service or you use Spring REST Framework.
Pavan Vadlamudi, geändert vor 10 Jahren.

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

New Member Beitrag: 1 Beitrittsdatum: 25.06.13 Neueste Beiträge
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.
Krzysztof Jastrzębski, geändert vor 7 Jahren.

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

New Member Beiträge: 2 Beitrittsdatum: 05.12.16 Neueste Beiträge
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?
Krzysztof Jastrzębski, geändert vor 7 Jahren.

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

New Member Beiträge: 2 Beitrittsdatum: 05.12.16 Neueste Beiträge
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
Milen Dyankov, geändert vor 7 Jahren.

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

Expert Beiträge: 310 Beitrittsdatum: 30.10.12 Neueste Beiträge
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
Holger García Fernández, geändert vor 11 Jahren.

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

New Member Beitrag: 1 Beitrittsdatum: 28.10.12 Neueste Beiträge
Could you please post your solution to this problem, I am struggling with this and haven´t find a suitable solution by myself.