Results 1 to 5 of 5

Thread: Problems with createFromJsonArray(@RequestBody String json)

  1. #1
    Join Date
    Aug 2011
    Posts
    5

    Default Problems with createFromJsonArray(@RequestBody String json)

    Hi,
    I am newbie in Spring Roo, so I have problems

    I have 3 domain classes

    Code:
    entity --class ~.domain.Lang --testAutomatically 
    field string --fieldName name --sizeMin 2 --sizeMax 20 --notNull
    
    entity --class ~.domain.Category --testAutomatically 
    field string --fieldName name --sizeMin 2 --sizeMax 30  --notNull
    
    entity --class ~.domain.Text --testAutomatically 
    field reference --fieldName lang --type ~.domain.Lang --notNull 
    field reference --fieldName category --type ~.domain.Category --notNull
    field string --fieldName title --notNull --sizeMax 100
    field string --fieldName author --sizeMax 100
    field string --fieldName text --notNull --sizeMax 200000 --sizeMin 1000

    I send by POST to
    http://localhost:8080/TextCuter/lang/jsonArray [{"name" : "en"}]
    and to
    http://localhost:8080/TextCuter/category/jsonArray [{"name" : "it"}]

    That works fine (and now I have category and language, both with id == 1)

    Then I want to create some Text
    I send to http://localhost:8080/text/lang/jsonArray [{"category" : 1, "lang" : 1, ...}]
    and it doesn't work... (validation failed)

    When I try to add this Text using browser and auto generated form it works well.

    What do I wrong?

  2. #2
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    It would depend on how your exact json document for lang looks like. To see how the document should look like you can first create a new lang via the MVC form and then do a json get to see the json version of that document. Based on this format you should be able to do a json post successfully. If that does not work it is probably best if you zip up your project (use the backup command) and attach it here or to a Jira ticket.
    Stefan Schmidt
    Software Engineer, Spring Roo
    SpringSource - a division of VMware
    twitter @schmidtstefan

  3. #3
    Join Date
    Aug 2011
    Posts
    5

    Default

    Hi Stefan,
    Thank you for answer, but i still have a problems.

    Quote Originally Posted by Stefan Schmidt View Post
    It would depend on how your exact json document for lang looks like. To see how the document should look like you can first create a new lang via the MVC form and then do a json get to see the json version of that document. Based on this format you should be able to do a json post successfully. If that does not work it is probably best if you zip up your project (use the backup command) and attach it here or to a Jira ticket.
    Sorry I made a mistake

    I sent to http://localhost:8080/text/learn/jsonArray [{"category" : 1, "lang" : 1, ...}]

    When I try to send to http://localhost:8080/text/learn with post data: ?category=1&lang=1&....(same other param) it works.

    JSON structure for ~domain.Text is: (return this when I request http://localhost:8080/TextCuter/learn/1")
    {
    "author":null,
    "category":{"id":1,"name":"bb","version":0},
    "id":1,
    "lang":{"id":1,"name":"aa","version":0},
    "text":"abcdefg abcdefg......",
    "title":"title",
    "version":0
    }


    There is no need to backup all project there is just few roo comands
    Code:
    persistence setup --database MYSQL --provider HIBERNATE --databaseName textcut
    
    entity --class ~.domain.Lang --testAutomatically 
    field string --fieldName name --sizeMin 2 --sizeMax 20 --notNull
    
    entity --class ~.domain.Category --testAutomatically 
    field string --fieldName name --sizeMin 2 --sizeMax 30  --notNull
    
    entity --class ~.domain.Text --testAutomatically 
    field reference --fieldName lang --type ~.domain.Lang --notNull 
    field reference --fieldName category --type ~.domain.Category --notNull
    field string --fieldName title --notNull --sizeMax 100
    field string --fieldName author --sizeMax 100
    field string --fieldName text --notNull --sizeMax 200000 --sizeMin 1000
    
    web mvc setup
    web mvc scaffold --class ~.web.LearnController --backingType ~.domain.Text --path /learn 
    web mvc scaffold --class ~.web.LanguageController --backingType ~.domain.Lang --path /language 
    web mvc scaffold --class ~.web.CategoryController --backingType ~.domain.Category --path category
    
    json all

  4. #4
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    Borius,

    You need to make sure the HTTP headers are set correctly when sending your JSON document. Furthermore, you need to define the identifiers of the referenced entities like this:

    Code:
    curl -i -X POST -H Accept:application/json -H Content-Type:application/json -d '{"author":"test author","category":{"id":2},"lang":{"id":1},"text":"test [......]","title":"test title"}'
    Hope this helps.
    Stefan Schmidt
    Software Engineer, Spring Roo
    SpringSource - a division of VMware
    twitter @schmidtstefan

  5. #5
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    I should mention it also works for JSON arrays the same way:

    Code:
    curl -i -X POST -H Accept:application/json -H Content-Type:application/json -d '[{"author":"test6","category":{"id":2},"lang":{"id":1},"text":"[...]","title":"test6"},{"author":"test5","category":{"id":2},"lang":{"id":1},"text":"test [...]","title":"test5"}]' http://localhost:8080/text/learn/jsonArray
    Stefan Schmidt
    Software Engineer, Spring Roo
    SpringSource - a division of VMware
    twitter @schmidtstefan

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
  •