Results 1 to 2 of 2

Thread: How to Return JSON after successful POST of a record

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    15

    Default How to Return JSON after successful POST of a record

    I have enabled json for my project with:
    web mvc json setup
    web mvc json all --package ~.web

    I am able to create record with curl such as
    curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '[{name: "Cheesy Crust"},{name: "Thick Crust"}]' http://localhost:8080/pizzashop/bases/jsonArray

    However, upon successful creation of a record, there seems to be no json in the response. I just see something like:

    HTTP/1.1 200 OK
    Server: Apache-Coyote/1.1
    Content-Type: application/json;charset=utf-8
    Content-Length: 831
    Date: Sun, 24 Feb 2013 16:44:44 GMT

    I want to see at least the id of the row I had just created. What's the recommended approach in roo? Modify the controller_Json.aj file? Or is there a better method?

    I've noticed that in the getters that roo created, it's throwing back <object>.toJsonArray(result). Do I need to do this for the create as well?


    @RequestMapping(headers = "Accept=application/json")
    @ResponseBody
    public ResponseEntity<String> AgendaItemController.listJson() {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    List<AgendaItem> result = agendaItemService.findAllAgendaItems();
    return new ResponseEntity<String>(AgendaItem.toJsonArray(result), headers, HttpStatus.OK);

  2. #2
    Join Date
    Jun 2008
    Location
    Philadelphia, PA, USA
    Posts
    212

    Default

    Quote Originally Posted by FreeCoffee View Post
    I have enabled json for my project with:

    @RequestMapping(headers = "Accept=application/json")
    @ResponseBody
    public ResponseEntity<String> AgendaItemController.listJson() {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    List<AgendaItem> result = agendaItemService.findAllAgendaItems();
    return new ResponseEntity<String>(AgendaItem.toJsonArray(result), headers, HttpStatus.OK);
    [/QUOTE]

    You're on the right track. Never edit the .aj files - they are auto generated. Instead, use STS to push-in refactor the MVC methods back INTO the controller class itself. (Or you could just cut and paste them in, and search/replace the classname. part out of the method declarations). Roo won't generate aspect methods for ones you've pushed in, so you can customize there.

    The ResponseEntity is the right class. I use it and it works. I don't think the create method by default brings back any JSON so you'd have to investigate there. According to the HATEOAS method of REST (I think) you can return a link to the caller, but Roo doesn't do that by default.

    You can also check out this new project by Oliver Gierke - Spring MVC HATEOAS - https://github.com/SpringSource/spring-hateoas - lots to chew on there.

    Ken
    Ken Rimple
    Chariot Solutions
    email: krimple@chariotsolutions.com
    work: www.chariotsolutions.com/education
    personal: www.rimple.com

    Author: Spring Roo in Action (Manning)
    MEAP Site: manning.com/rimple

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
  •