Foros de discusión

Using Spring to map a RESTful webservice call onto a controller

Adam Hardy, modificado hace 11 años.

Using Spring to map a RESTful webservice call onto a controller

New Member Mensajes: 19 Fecha de incorporación: 18/05/12 Mensajes recientes
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, modificado hace 11 años.

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

New Member Mensajes: 19 Fecha de incorporación: 18/05/12 Mensajes recientes
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, modificado hace 11 años.

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

New Member Mensajes: 4 Fecha de incorporación: 28/04/11 Mensajes recientes
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, modificado hace 10 años.

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

New Member Mensaje: 1 Fecha de incorporación: 25/06/13 Mensajes recientes
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, modificado hace 7 años.

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

New Member Mensajes: 2 Fecha de incorporación: 5/12/16 Mensajes recientes
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, modificado hace 7 años.

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

New Member Mensajes: 2 Fecha de incorporación: 5/12/16 Mensajes recientes
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, modificado hace 7 años.

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

Expert Mensajes: 310 Fecha de incorporación: 30/10/12 Mensajes recientes
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, modificado hace 11 años.

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

New Member Mensaje: 1 Fecha de incorporación: 28/10/12 Mensajes recientes
Could you please post your solution to this problem, I am struggling with this and haven´t find a suitable solution by myself.