Search code examples
jsongrailsmicronaut

Micronaut JSON post strip the Quotes


In Micronaut Controller parsing the post request using JSON object. I expect it to not include quotes, but it quotes in the database insert.

Posting like this:

curl -X POST --header "Content-Type: application/json" -d '{"bookid":3,"name":"C++"}'  http://localhost:8880/book/save

Saving like this:

String bookid=JSON?.bookid
  String name=JSON?.name
def b =bookService.save(bookid,name

in database It stores like this:

+--------+-------+
| bookid | name  |
+--------+-------+
| 3      | "C++" |
+--------+-------+

I expect book name just C++

Thanks SR


Solution

  • You haven't provided enough information about your project to know what is going on but the project at https://github.com/jeffbrown/sfgroupsjsonbinding/tree/master demonstrates how the built in binding stuff works. See the README.md file there.

    https://github.com/jeffbrown/sfgroupsjsonbinding/blob/3ff4e8b39ba5fda9956ebfc67cd0b9e5d940b8f2/src/main/groovy/sfgroupsjsonbinding/BookController.groovy

    package sfgroupsjsonbinding
    
    import io.micronaut.http.annotation.Controller
    import io.micronaut.http.annotation.Get
    import io.micronaut.http.annotation.Post
    
    @Controller('/book')
    class BookController {
    
        private PersonService personService
    
        BookController(PersonService personService) {
            this.personService = personService
        }
    
        @Get('/')
        List<Person> list() {
            personService.list()
        }
    
        @Post('/')
        Person save(Person person) {
            personService.save person
        }
    
        @Get('/{id}')
        Person get(long id) {
            personService.get id
        }
    }
    

    Interacting With The App

     $ curl -H "Content-Type: application/json" -d '{"name":"Jeff"}' http://localhost:8080/book
    {"name":"Jeff","id":1}
     $ 
     $ curl -H "Content-Type: application/json" -d '{"name":"Jake"}' http://localhost:8080/book
    {"name":"Jake","id":2}
     $ 
     $ curl -H "Content-Type: application/json" -d '{"name":"Zack"}' http://localhost:8080/book
    {"name":"Zack","id":3}
     $ 
     $ curl http://localhost:8080/book
    [{"name":"Jeff","id":1},{"name":"Jake","id":2},{"name":"Zack","id":3}]
     $ 
     $ curl http://localhost:8080/book/1
    {"name":"Jeff","id":1}
     $ 
     $ curl http://localhost:8080/book/2
    {"name":"Jake","id":2}
     $ 
     $ curl http://localhost:8080/book/3
    {"name":"Zack","id":3}
     $