Announcement Announcement Module
Collapse
No announcement yet.
How to Return JSON after successful POST of a record Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment

    Working...
    X