Announcement Announcement Module
Collapse
No announcement yet.
Incorrect Accept in JSON create methods Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Incorrect Accept in JSON create methods

    Currently when using the @RooJson annotation the json create method @RequestMapping headers in the Controller aspect are set as headers = "Accept=application/json" and are returning new ResponseEntity<String>("myThing created", HttpStatus.CREATED).

    I believe this is incorrect and that the @RequestMapping headers should be headers = "Accept=text/plain".

    This however will probably clash with the default create method for the aspect so it may be better to change the method to return the json format of the created object e.g.

    @RequestMapping(method = RequestMethod.POST, headers = "Accept=application/json")
    @ResponseBody
    public String MyThingController.createFromJson(@RequestBody final String json) {
    MyThing myThing = MyThing.fromJsonToMyThing(json);
    myThing.persist();
    return myThing.toJson();
    }
    I believe it will work to change the headers for the createFromJsonArray method as it is the only method mapped to path /jsonArray. You could of course also change this method to return the json format of the created objects e.g.

    @RequestMapping(value = "/jsonArray", method = RequestMethod.POST, headers = "Accept=application/json")
    @ResponseBody
    public final String MyThingController.createFromJsonArray(@RequestBody final String json) {
    List<MyThing> myThings = new ArrayList<MyThing>();
    for (MyThing myThing : MyThing.fromJsonArrayToMyThing(json)) {
    myThing.persist();
    myThings.add(myThing);
    }
    return MyThing.toJsonArray(myThings);
    }
    Environment:
    Mac OS X 10.6.5
    roo-1.1.0.RELEASE
    sts-2.5.0.RELEASE
    Last edited by CeartShamus; Nov 21st, 2010, 12:32 AM. Reason: Have attached an untested update of WebScaffoldMetadata.java which may rectify the issue

  • #2
    Sean, I am unclear why you consider the header mapping wrong here. See my comment here https://jira.springframework.org/browse/ROO-1758.

    Comment


    • #3
      I think I might understand what is being said, but take it with a grain of salt, I may be way off base:

      Code:
          @RequestMapping(value = "/jsonArray", method = RequestMethod.POST, headers = "Accept=application/json")
          public ResponseEntity<String> PetController.createFromJsonArray(@RequestBody String json) {
          	
              for (Pet pet: Pet.fromJsonArrayToPets(json)) {
                  pet.persist();
              }
              return new ResponseEntity<String>("Pet created", HttpStatus.CREATED);
          }
      RequestMethod.POST - Means I am posting data to the controller
      headers="Accept=application/json" - Meanins I that data I am POSTing will be in json format.

      There isn't a setting for saying what format you want the response to be.
      The default response is to return a text/plain response stating the items were created.


      I can see how you might want a different response like a json/response that lists the course Id's for the courses that were created. But that would require you to create another parameter or something to state you would rather have a json response with the id's of what was created.

      I don't believe it has anything to do with the content-type and header accepts for the Request.

      So if you wanted the json POST response to be a json response with what was created, then you change the default method created or add a new one to have a different response, or like below add another version of the POST request, that returns a json response.

      Code:
             @RequestMapping(value = "/jsonArray", method = RequestMethod.POST, headers = "Accept=application/json")
          @ResponseBody
          public String PetController.createFromJsonArray(@RequestBody String json) {
          	Collection<Pet> petsCreated = new Collection<Pet>();
              for (Pet pet: Pet.fromJsonArrayToPets(json)) {
                  pet.persist();
                  petsCreated.add(pet);
              }
              return Pet.toJsonArray(petsCreated);
          }
      Again, I am no expert by any means, but from what I understand the default response to sending in a json string of items to created is just a string response saying they were created. If you want more details coming back then you have to add or modify the POST request you are using to get the data in there.

      Anyway, my attempt to help out

      Comment


      • #4
        Hi btlife,

        Thanks for replying

        It is my understanding that the Accept request-header field is used to specify certain media types which are acceptable for the response; where content-type is used to indicate the media type of the body sent to the recipient.

        i.e. specifying Accept=application/json means you will accept a json response and setting Content-Type as application/json means you will be supplying json content.

        https://jira.springsource.org/browse/ROO-1758

        Comment


        • #5
          Originally posted by CeartShamus View Post
          Hi btlife,

          Thanks for replying

          It is my understanding that the Accept request-header field is used to specify certain media types which are acceptable for the response; where content-type is used to indicate the media type of the body sent to the recipient.

          i.e. specifying Accept=application/json means you will accept a json response and setting Content-Type as application/json means you will be supplying json content.

          https://jira.springsource.org/browse/ROO-1758

          Ok cool deal, learn something new everyday, thanks!!

          Comment


          • #6
            We fixed this by returning null (along with the correct HTTP header) instead of a string.

            Comment

            Working...
            X