Announcement Announcement Module
Collapse
No announcement yet.
Roo and RESTful web services example Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Roo and RESTful web services example

    Hello everyone,

    I'm a dummy, new to roo and Spring overall, so therefore my questions might be very stupid or simple. I found roo very interesting and a great way to start Spring for a newbie, but I feel I don't know where to begin with with the following:

    I want to extend one of the available examples, say PetClinic, and allow it to add VISITS through a web service (not exactly the current for and JSPX generated by roo). My client will be a mobile J2ME device with which I will send a VISIT and my PetClinic project should allow it to record it.

    I know this is easy, but from my standing point, I have no clue on what to do! Should I modify the .aj controller, or directly the .java controller creating a new RequestMethod.POST?

    Thank you!!

  • #2
    Hello,

    Spring Roo is a great place to start, but you should realize that its just one way to come up with a working Spring 3.0 web-application.

    You will need to look into how Spring works to do many things. It depends what your requirement is but you will benefit alot in the long run from reading the reference documentation.

    You may like to see http://blog.springsource.com/2010/01...in-spring-3-0/. This page alone may not be helpful to answer all your questions, but you will get the idea from the reference documentation.

    Also look for ContentNegotiatingViewResolver and how to set it up (one example is here http://forum.springsource.org/showth...ngViewResolver)

    this will make your restful URLs put out json files.

    Comment


    • #3
      Thank you Hatim,

      I'll have a look and see what I can do.

      Juan

      Comment


      • #4
        Hey there, I'm trying to add something like this on my ROO generated .aj controller file:

        Code:
        @RequestMapping(value = "/sitelocationvisit/{secGuard}/{siteLoc}", method = RequestMethod.GET)
            public String store(@PathVariable("secGuard") String securityGuard, @PathVariable("siteLoc") String siteLocation) {
        		SecurityGuard sg = SecurityGuard.findSecurityGuard(1L); //securityGuard
        		SiteLocation sl = SiteLocation.findSiteLocation(1L); //siteLocation
        		SiteLocationVisit slv = new SiteLocationVisit();
        		slv.persist();
        		System.out.println("listooo");
                return "redirect:/sitelocationvisit/" + slv.getId();
            }
        I want to be able to receive 2 parameters, and pass them to my entity and then store this new entity. I don't know buy I get this error:

        Request method 'GET' not supported
        and this

        org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter$ServletHandlerMethodRe solver.resolveHandlerMethod(AnnotationMethodHandle rAdapter.java:567)
        org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.invokeHandlerMethod(An notationMethodHandlerAdapter.java:409)
        org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho dHandlerAdapter.java:402)
        org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:771)
        org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:716)
        org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:647)
        org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:552)
        javax.servlet.http.HttpServlet.service(HttpServlet .java:617)
        javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
        org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
        org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
        org.apache.catalina.core.ApplicationDispatcher.inv oke(ApplicationDispatcher.java:646)
        org.apache.catalina.core.ApplicationDispatcher.pro cessRequest(ApplicationDispatcher.java:436)
        org.apache.catalina.core.ApplicationDispatcher.doF orward(ApplicationDispatcher.java:374)
        org.apache.catalina.core.ApplicationDispatcher.for ward(ApplicationDispatcher.java:302)
        org.tuckey.web.filters.urlrewrite.NormalRewrittenU rl.doRewrite(NormalRewrittenUrl.java:195)
        org.tuckey.web.filters.urlrewrite.RuleChain.handle Rewrite(RuleChain.java:159)
        org.tuckey.web.filters.urlrewrite.RuleChain.doRule s(RuleChain.java:141)
        org.tuckey.web.filters.urlrewrite.UrlRewriter.proc essRequest(UrlRewriter.java:90)
        org.tuckey.web.filters.urlrewrite.UrlRewriteFilter .doFilter(UrlRewriteFilter.java:417)
        org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
        org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
        org.springframework.web.filter.HiddenHttpMethodFil ter.doFilterInternal(HiddenHttpMethodFilter.java:7 1)
        org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)
        org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
        org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
        org.springframework.web.filter.CharacterEncodingFi lter.doFilterInternal(CharacterEncodingFilter.java :88)
        org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)
        org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
        org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
        org.springframework.orm.jpa.support.OpenEntityMana gerInViewFilter.doFilterInternal(OpenEntityManager InViewFilter.java:113)
        org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)
        org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
        org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
        org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
        org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)
        com.springsource.metrics.collection.web.HttpReques tMetricCollectionValve.invoke(HttpRequestMetricCol lectionValve.java:44)
        org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:128)
        org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102)
        org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
        org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:293)
        org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:849)
        org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:583)
        org.apache.tomcat.util.net.JIoEndpoint$SocketProce ssor.run(JIoEndpoint.java:379)
        java.util.concurrent.ThreadPoolExecutor$Worker.run Task(ThreadPoolExecutor.java:886)
        java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:908)
        java.lang.Thread.run(Thread.java:619)
        Anybody has clue no what's going on? Should I modify some config files to make the mapping work?

        Thank you!!!

        Comment


        • #5
          You should add to an existing or create a new Java controller, not modify the aspect. Roo's aspects are managed by it so your work could get overwritten.

          This is probably not your immediate problem however--suggest you refer to Spring 3.0 MVC (REST) docs and samples.

          Comment


          • #6
            Hi Rod,

            You are right, I knew I shouldn't modify the roo generated .aj files. I'm doing the following:

            1) I create a new mapping on an existing entity controller (.java):

            Code:
            @RooWebScaffold(path = "securityguard", automaticallyMaintainView = true, formBackingObject = SecurityGuard.class)
            @RequestMapping("/securityguard/**")
            @Controller
            public class SecurityGuardController {
            	
            	@RequestMapping(value = "/securityguard/mobile/{secGuardName}", method = RequestMethod.GET)
                public String store(@PathVariable("secGuardName") String securityGuard, 
                		ModelMap modelMap) {
            		System.out.println("ahi entro");
            		SecurityGuard sg = new SecurityGuard();
            		sg.setFirstName(securityGuard);
            		sg.persist();
                    modelMap.addAttribute("securityguards", SecurityGuard.findAllSecurityGuards());
                    return "securityguard/list";
                }
            	
            }
            2) Once there is a request on that URI, the entity is stored properly.

            3) However, when I click on a "create new +entityName" on the roo generated web page, I get this error:

            Requested Resource Not Found
            Sorry, we did not find the resource you were looking for.
            No matching handler method found for servlet request: path '/securityguard/form', method 'GET', parameters map[[empty]]
            org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter$ServletHandlerMethodRe solver.resolveHandlerMethod(AnnotationMethodHandle rAdapter.java:571)
            org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.invokeHandlerMethod(An notationMethodHandlerAdapter.java:409)
            org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho dHandlerAdapter.java:402)
            org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:771)
            org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:716)
            org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:647)
            org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:552)
            javax.servlet.http.HttpServlet.service(HttpServlet .java:617)
            javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
            org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
            org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
            org.apache.catalina.core.ApplicationDispatcher.inv oke(ApplicationDispatcher.java:646)
            org.apache.catalina.core.ApplicationDispatcher.pro cessRequest(ApplicationDispatcher.java:436)
            org.apache.catalina.core.ApplicationDispatcher.doF orward(ApplicationDispatcher.java:374)
            org.apache.catalina.core.ApplicationDispatcher.for ward(ApplicationDispatcher.java:302)
            org.tuckey.web.filters.urlrewrite.NormalRewrittenU rl.doRewrite(NormalRewrittenUrl.java:195)
            org.tuckey.web.filters.urlrewrite.RuleChain.handle Rewrite(RuleChain.java:159)
            org.tuckey.web.filters.urlrewrite.RuleChain.doRule s(RuleChain.java:141)
            org.tuckey.web.filters.urlrewrite.UrlRewriter.proc essRequest(UrlRewriter.java:90)
            org.tuckey.web.filters.urlrewrite.UrlRewriteFilter .doFilter(UrlRewriteFilter.java:417)
            org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
            org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
            org.springframework.web.filter.HiddenHttpMethodFil ter.doFilterInternal(HiddenHttpMethodFilter.java:7 1)
            org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)
            org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
            org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
            org.springframework.web.filter.CharacterEncodingFi lter.doFilterInternal(CharacterEncodingFilter.java :88)
            org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)
            org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
            org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
            org.springframework.orm.jpa.support.OpenEntityMana gerInViewFilter.doFilterInternal(OpenEntityManager InViewFilter.java:113)
            org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)
            org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
            org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
            org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
            org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)
            com.springsource.metrics.collection.web.HttpReques tMetricCollectionValve.invoke(HttpRequestMetricCol lectionValve.java:44)
            org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:128)
            org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102)
            org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
            org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:293)
            org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:849)
            org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:583)
            org.apache.tomcat.util.net.JIoEndpoint$SocketProce ssor.run(JIoEndpoint.java:379)
            java.util.concurrent.ThreadPoolExecutor$Worker.run Task(ThreadPoolExecutor.java:886)
            java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:908)
            java.lang.Thread.run(Thread.java:619)

            I think creating that new mapping on the .java files is somehow overwriting the properly mappings of the aj. file for that particular entity. Is there something I'm missing here?

            Thank you,

            Juan

            Comment


            • #7
              Can you check in the aspect (aj) for your controller if the method / mapping for /securityguard/form still exists?

              It looks like you accidently removed it. Starting the Roo shell on your project again should restore the correct aj file for your controller.

              HTH,
              Stefan

              Comment


              • #8
                Hi Stefan,

                It's really weird. I never stopped roo and it was always running. As you said, I made sure that it dit not erase my roo .aj controller methods. So the mistery is still there, I will try to investigate further and will let you know if I find something.

                To summarize, this happened: I could not create a new controller method for an entity in the .java class. It somehow messed some existing controller mappings created on the auto-generated .aj file.

                What I did temporarily was creating a new MyController.java class and referenced my URI there and the entities I wanted to work with. Not elegant at all, because it does not make sense to create yet another controller, but this worked... for now.

                Thank you for the help!

                Comment


                • #9
                  I ran into the same problem

                  First upgrade to the latest version of Spring 3.

                  (pom.xml)
                  Code:
                  	<properties>
                  		<spring.version>3.0.1.RELEASE</spring.version>
                  		<aspectj.version>1.6.6.RELEASE</aspectj.version>
                  	</properties>
                  Next, include jackson, your problem should clear up and you should be blessed with wonderful JSON payloads.


                  (pom.xml)

                  Code:
                  	<!-- Jackson JSON Mapper -->
                  		<dependency>
                  			<groupId>org.codehaus.jackson</groupId>
                  			<artifactId>jackson-mapper-asl</artifactId>
                  			<version>1.4.2</version>
                  		</dependency>
                  Sample controller that delivers JSON payloads:

                  Code:
                  package com.somecompany.appcatalog.web;
                  
                  import java.util.List;
                  
                  import javax.servlet.http.HttpServletResponse;
                  
                  import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
                  import com.somecompany.appcatalog.domain.Application;
                  
                  import org.springframework.web.bind.annotation.PathVariable;
                  import org.springframework.web.bind.annotation.RequestMapping;
                  import org.springframework.web.bind.annotation.RequestMethod;
                  import org.springframework.web.bind.annotation.RequestParam;
                  import org.springframework.web.bind.annotation.ResponseBody;
                  import org.springframework.stereotype.Controller;
                  
                  @RooWebScaffold(path = "application", automaticallyMaintainView = true, formBackingObject = Application.class)
                  @RequestMapping("/application/**")
                  @Controller
                  public class ApplicationController {
                  	
                  	@RequestMapping(value = "/application/hello", method = RequestMethod.GET)
                  	public @ResponseBody Application hello(@RequestParam String name, HttpServletResponse response) {
                  		if (name == null) {
                  			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                  			return null;
                  		} else {
                  			return new Application("com.foo.myapp", "foo.bar", "Cool small app", true);
                  		}
                  	}
                  	
                  	@RequestMapping(value = "/application/hello2/{name}", method = RequestMethod.GET)
                  	public @ResponseBody Application hello2(@PathVariable String name, HttpServletResponse response) {
                  		if (name == null) {
                  			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                  			return null;
                  		} else {
                  			return new Application("com.foo.myapp", "foo.bar", "Cool small app", true);
                  		}
                  	}
                  
                  	
                  	@SuppressWarnings("unchecked")
                  	@RequestMapping(value = "/application/appList", method = RequestMethod.GET)
                  	public @ResponseBody List<Application> getAppList(@RequestParam String publicId, HttpServletResponse response) {
                  	       if (publicId == null || publicId.length() == 0) throw new IllegalArgumentException("A PublicId is required.");
                  	       return Application.findApplicationsByPublicIdLike(publicId).getResultList()   ;
                  	 }
                  
                  
                  }

                  Comment


                  • #10
                    Tranks

                    Tranks that work for me

                    now Json Rocks

                    Great Tip

                    Comment


                    • #11
                      Sorry to revive an old thread.

                      You may find some more info on this subject at

                      http://www.youtube.com/user/BTI360#p/a/u/1/xLWm8PJjdBk - RESTful Web Services with Spring Part 1 of 3
                      but more importantly

                      http://www.youtube.com/watch?v=TNE7NBLseRA RESTful Web Services with Spring Roo

                      Comment

                      Working...
                      X