Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Spring RESTful Client/Server tutorial

Hybrid View

  1. #1

    Arrow Spring RESTful Client/Server tutorial

    Hi all,

    I am looking to see if anyone knows of any good blogs/tutorials on exposing RESTful URL's both on the server and then communicating with them on a client using Spring 3?

    I am comfortable with Spring MVC and the various annotations and after reading the following:

    http://blog.springsource.com/2009/03...-spring-3-mvc/
    http://blog.springsource.com/2009/03...-resttemplate/

    I was wondering if anyone could point out a full blown example? Or is there an example of such a thing in the Spring 3 distribution?

    Many thanks in advance for the pointers

    Eggsy

  2. #2

    Default

    In brief:


    Server uses default annotated controllers with @PathVariable annotation:
    Code:
    @Controller
    public class MyController {
    
        @Autowired
        Service myService;
    
        @RequestMapping(value="/path/{word}", method=RequestMethod.GET)
        public ModelAndView myRestMethod(@PathVariable String word) {
    
            ResponseObj response = myService.getResponse(word);
            return new ModelAndView("jaxbView", BindingResult.MODEL_KEY_PREFIX + "response", response);
        }
    }
    The controller requires a view named "jaxbView" and a viewresolver to resolve the same:
    Code:
        <!-- Scan for controllers -->
        <context:component-scan base-package="my.controller.package" />
    
    
        <!-- Resolve views based on string names -->
        <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
    
    
        <!-- XML view using a JAXB2 marshaller -->
        <bean id="jaxbView" class="org.springframework.web.servlet.view.xml.MarshallingView">
            <constructor-arg ref="jaxbMarshaller" />
        </bean>
    
    
        <!-- JAXB2 marshaller. Automagically turns beans into xml -->
        <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="classesToBeBound">
                <list>
                    <value>my.package.ResponseObj</value>
                </list>
            </property>
            <!-- Possibly include schema for validation -->
            <property name="schema" value="classpath:schema.xsd"/>
        </bean>

    Finally, the object we wish to expose (my.package.ResponseObj)
    Code:
    @XMLRootElement
    public class ResponseObj {
       private String something;
    
       public void setSomething(String s) {...}
       public String getSomething() {...}
    }

    That was the entire server section. Other than that, web.xml needs to be configuered just like any other app which uses annotated controllers.






    Then, the client side is just as slick. First the REST client imlementation:
    Code:
    public class restClient {
        RestTemplate restTemplate;
    
        public void fetchRESTObject() {
            ResponseObj obj = (ResponseObj) restTemplate.getForObject("http://url/myService/{param}", ResponseObj.class, "myParameterWord");
        }
    
        [...] 
    }

    Then the spring configuration to load the rest client
    Code:
        <!-- My REST client injected with spring RestTemplate -->
        <bean id="restClient" class="test.RestClient">
            <property name="restTemplate" ref="restTemplate"/>
        </bean>
    
    
        <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
            <property name="messageConverters">
                <list>
                    <bean id="messageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                        <property name="marshaller" ref="xmlMarshaller" />
                        <property name="unmarshaller" ref="xmlMarshaller" />
                    </bean>
                </list>
            </property>
        </bean>
    
    
       <bean id="xmlMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller" >
            <property name="classesToBeBound">
                <list>
                    <value>my.package.ResponseObj</value>
                </list>
            </property>
            <!-- Possibly use schema validation -->
            <property name="schema" value="schema.xsd" />
        </bean>


    Note how both endpoints use the same ResponseObj that I created. I simply copied the sources from the server to the client, although, any class having the same layout can act as a substitute.

    Client classes can even be generated from an XSD file. Note how maven and the xjc plugin can be used to be generate java to xsd on the server side, and then xsd to java on the client side. It is however doubtful that a REST-service should at all need an XSD file. The XML in itself and dokumentation should be enough.

    Lastly, note how the JAXB2 marshaller can be replaced by any other mashaller, such as XStream, Castor, XMLBeans etc.


    That was all, hope it was helpful
    Last edited by Toxic; Dec 23rd, 2009 at 10:44 AM.

  3. #3

    Wink Great

    Hi there

    thank you very much for such a great and in depth reply.

    I'll read over it and see how I get on!

    Thanks again

  4. #4

    Smile Blog post on subject

    Hi there,

    Just to let you know I have compiled a quick blog post tutorial on this subject at:

    http://eggsylife.co.uk/2010/01/03/sp...-web-services/

    Example code can be downloaded at:

    http://code.google.com/p/eggsy-spring3-restful-example/

    (Toxic I have mentioned this you and this post on the blog post)

  5. #5

    Default

    Nice post. I'm glad it worked out for you



    As a minor side note; when using XML schemas to construct a contract-first RESTful service, it helps to solve some of the potential problems with JAXB2 by using property contextPath instead of classesToBeBound in the jaxbMarshaller on the client side.

  6. #6
    Join Date
    Oct 2009
    Posts
    3

    Default

    Example code can be downloaded at:

    http://code.google.com/p/eggsy-spring3-restful-example/

    I am not able to Check out the code from GoogleCode. Can you tell whether the SourceCode location is moved.

    URL Accessed to Download the Source :http://eggsy-spring3-restful-example...com/svn/trunk/ eggsy-spring3-restful-example-read-only

  7. #7
    Join Date
    May 2011
    Posts
    4

    Default

    That was a great post. Nice work. Even two years later, it is one of the more concise write-ups I have seen.

  8. #8

    Default

    Great stuff helped alot
    Realtionships are tough, We all know that.. Whats harder is break ups. GO TO MY BLOG TO LEARN MORE:
    Get A EX BACK

  9. #9
    Join Date
    Feb 2011
    Posts
    3

    Default Having a view problem

    I used this example and I have a problem. here is the error stack :

    javax.servlet.ServletException: Could not resolve view with name 'jaxbView' in servlet with name 'LabResult'
    org.springframework.web.servlet.DispatcherServlet. render(DispatcherServlet.java:1029)
    org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:817)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:549)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:803)

  10. #10

    Default

    Quote Originally Posted by Itf View Post
    I used this example and I have a problem. here is the error stack :

    javax.servlet.ServletException: Could not resolve view with name 'jaxbView' in servlet with name 'LabResult'
    org.springframework.web.servlet.DispatcherServlet. render(DispatcherServlet.java:1029)
    org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:817)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:549)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:803)


    You need two things:

    1.

    <!-- Resolve views based on string names -->
    <bean class="org.springframework.web.servlet.view.BeanNa meViewResolver" />


    2.

    <!-- XML view using a JAXB2 marshaller -->
    <bean id="jaxbView" class="org.springframework.web.servlet.view.xml.Ma rshallingView">
    <constructor-arg ref="jaxbMarshaller" />
    </bean>


    And

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •