Results 1 to 4 of 4

Thread: Spring Roo REST XML POST

  1. #1
    Join Date
    Nov 2010
    Location
    Santo Domingo, Albay Philippines
    Posts
    158

    Default Spring Roo REST XML POST

    I just finished the great article resting-with-roo-adding-content-negotiation-and-rest-in-two

    And Now get the view for HTML, XML and JSON out of my application with another application. So the next step is to turn it around.

    I can now fetch data from the application in XML or JSON with another application, but I cannot find documentation or articles or tutorial on how to leverage the same entities and do RESTful POST and PUT to put data into the application in XML or JSON.

    Can anyone point me in the right direction?
    Last edited by MikeOliverAZ; Jul 17th, 2011 at 07:29 PM. Reason: link broken

  2. #2
    Join Date
    Nov 2010
    Location
    Santo Domingo, Albay Philippines
    Posts
    158

    Question Refining the question.

    Quote Originally Posted by MikeOliverAZ View Post

    I can now fetch data from the application in XML or JSON with another application, but I cannot find documentation or articles or tutorial on how to leverage the same entities and do RESTful POST and PUT to put data into the application in XML or JSON.
    I found the following http://www.ibm.com/developerworks/we...erv/index.html to be quite good.

    But in comparing sources I am closer but still not quite there yet.

    For example in the above it shows the controller with @RequestMapping for POST to be

    Code:
    @RequestMapping(method=RequestMethod.POST, value="/employee")
    public ModelAndView addEmployee(@RequestBody String body) {
    Source source = new StreamSource(new StringReader(body));
    Employee e = (Employee) jaxb2Mashaller.unmarshal(source);
    employeeDS.add(e);
    return new ModelAndView(XML_VIEW_NAME, "object", e);
    }
    Where my Roo controller aspect @RequestMapping for POST to be

    Code:
     @RequestMapping(method = RequestMethod.POST)
        public String HZoneController.create(@Valid HZone HZone, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
            if (bindingResult.hasErrors()) {
                uiModel.addAttribute("HZone", HZone);
                return "hzones/create";
            }
            uiModel.asMap().clear();
            HZone.persist();
            return "redirect:/hzones/" + encodeUrlPathSegment(HZone.getId().toString(), httpServletRequest);
        }
    Given the warning on the Roo_Controller.aj

    // WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
    I am wondering the best course of action to implement such that the Roo JSP views, form posts, etc. are retained, while adding REST XML AND REST JSON, POST AND PUT.

    It seems to me the easiest to just have different @RequestMappings for different URI paths where one would be the current mapping is unchanged and new mappngs made for XML = /hzones/xml/ and JSON = /hzones/json/

    Or is it better to create three controllers and leave the Roo Controller untouched?

  3. #3
    Join Date
    Dec 2009
    Location
    West Chester, Pennsylvania USA
    Posts
    36

    Default

    Do not modify the *.aj files. If you are using STS/Eclipse, select the method from Package Explorer and choose Refactor > Push In... this will push the code from the Intertype Declaration to your class for safe editing.

    The code from developerworks is ok, but very dated. With annotations @RequestBody & @ResponseBody the data that is passed is automatically marshalled based on the configured marshaller.

    Configure <mvc:annotation-driven/> in your servlet context xml file.
    Include JaxB2Marshaller configuration in your servlet context xml file, the one built into core spring, not from spring-ws 1.x.
    Annotate the classes for JaxB2.
    Include the JaxB2 jars in the project classpath (Maven POM)

    Although my blog is about JMS, the same configuration for JAXB2 is valid for MVC/REST - http://gordondickens.com/wordpress/2...h-jmstemplate/

    Regards,
    Gordon Dickens

    twitter.com/gdickens
    linkedin.com/in/gordondickens
    Blog: technophile.gordondickens.com
    Last edited by gdickens; Jul 21st, 2011 at 06:32 AM.
    Regards,
    Gordon Dickens

    gordon@gordondickens.com
    twitter.com/gdickens
    linkedin.com/in/gordondickens
    Blog: technophile.gordondickens.com

  4. #4
    Join Date
    Nov 2010
    Location
    Santo Domingo, Albay Philippines
    Posts
    158

    Default Excellent, thanks

    Yes I was arriving at that with considerable effort looking around, who knows how long it would have taken me to find it on my own.

Posting Permissions

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