Micronaut JSON post strip the Quotes

micronaut http client post example
micronaut client
micronaut filter example
micronaut httpresponse example
micronaut configuration
micronaut intellij
micronaut replaces
micronaut embedded server

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

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}
 $ 

Micronaut JSON post strip The Quotes, Micronaut JSON post strip The Quotes. В контроллер Micronaut разбора POST-​запрос с помощью объекта JSON. Я ожидаю, что он не будет включать  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.

I know i am a bit late but i've been searching for the solution for long and didnt find anything. After a lot of effort i found that sending jackson "Object" with "@BODY" is not helpful, when i changed the the type to "String" it worked for me.

Here is my Code
def save(@Body String JSON){

    def result = [:]
    ObjectMapper objectMapper = new ObjectMapper();

    //convert json string to object
    def obj = objectMapper.readValue(JSON, Course.class);
    println(obj)

    Course course = new Course(name: obj?.name, pre: obj?.pre,
            regno: obj?.regno, enrolled: obj?.enrolled)

    course.validate()
    if(course.hasErrors()){
        println("Error: "+course.errors)
        result.put("Error is: ",course.errors)
        return result;
    }

    course.save(flush:true,failOnError: true)

    result.put("Message","Successfully Created")
    result.put("Result",course)
    return HttpResponse.created(result)
}

Passing it to ObjectMapper and then converting it from JSON string to Java Object worked for me.

Json string that i passed is as follow:
{
 "name" : "Data Structures",
 "pre" : "Computer Programming",
 "regno" : "249",
 "enrolled" : "90"
}

Here is the storing of data before and after change in database:

+----+---------+------------------------+-------------------------------+----+
| id | version |        name      |   pre       |     regno        |enrolled |
+----+---------+------------------------+-------------------------------+----+
|  1 | 0 | "Computer Programming" | "Introduction to Programming"| "233"|"26"|
|  2 | 0 | Data Structures        | Computer Programming         |  249 | 90 |
+----+---------+------------------------+-------------------------------+----+

Hope the answer helps out anyone who is looking for alternate solution to above solution.

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:. Suppose we want our controller methods to return a JSON response when the HTTP Accept header is set to application/json and XML when the Accept header is set to application/xml. We can access the values of HTTP headers in our controller methods by adding an argument of type HttpHeaders to our method definition and Micronaut will add all HTTP

It seems my command class extending some other class was causing some issues which meant nothing worked. At the moment it does work and is really a working alternative to what Zaryab baloch had suggested above.

package gateway.command.controller;


import com.fasterxml.jackson.annotation.JsonProperty;
import gateway.command.event.commands.HotelSaveCommand;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.codec.MediaTypeCodecRegistry;
import io.micronaut.jackson.codec.JsonMediaTypeCodec;
import lombok.extern.slf4j.Slf4j;

import javax.inject.Inject;

@Slf4j
@Controller("/")
public class GatewayController  {

    @Inject
    protected MediaTypeCodecRegistry mediaTypeCodecRegistry;

    /**
     *
     * @param topic
     * @param eventType using the jsonProperty we actually extract eventType from the @Body string JSON String
     *                  The 3rd input is actual form. we post /hotel and json content there isn't actually 3 parameters
     *                  provided
     * @param
     * @return
     */
    @Post(uri = "/{topic}", consumes = MediaType.APPLICATION_JSON)
    public HttpResponse process(String topic, @JsonProperty("eventType") String eventType, @Body String formInput)  {

        JsonMediaTypeCodec mediaTypeCodec = (JsonMediaTypeCodec) mediaTypeCodecRegistry.findCodec(MediaType.APPLICATION_JSON_TYPE)
                .orElseThrow(() -> new IllegalStateException("No JSON codec found"));

        HotelSaveCommand command =  mediaTypeCodec.decode(HotelSaveCommand.class,formInput);
        if (command!=null) {
            System.out.println(command +" "+ command.getName());
        }
        //eventPublisher.publish(topic,);
        return HttpResponse.accepted();
    }

}

Micronaut Framework/questions, 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: You can try using regex to remove quotes from your string. var strWithOutQuotes= strWithQuotes.replace(/['"]+/g, '') ['"] is a character class, matches both single and double quotes. you can replace this with "to only match double quotes. +: one or more quotes, chars, as defined by the preceding char-class (optional) g: the global flag. This tells JS to apply the regex to the entire string.

Micronaut Views, stackoverflow.com/questions/54000199/micronaut-json-post-strip-the-quotes @navhan https://docs.micronaut.io/latest/guide/index.html#formData 9 more Error: Image build request failed with exit status 1. using micronaut-kafka 1.4.0. Elvis Souza. @mageddo. It appears, remove io.micronaut.configuration.kafka.​graal. Micronaut Stock Images Our stock images are available upon request and cover current medical issues, pharmacology, biotechnology, chemistry, biology, and other areas of life science. In general you will be granted a license for ‘ONE TIME & NON-EXCLUSIVE USE’ – meaning that you can use the image for one type of publication or web domain only.

Micronaut, 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: Running on micronaut 1.2.6 and Kotlin 1.3.50. Consider the following snippet: @Client("\${formValidatorService.host}") interface FormValidatorServiceClient { @Post

micronaut-projects/micronaut-core, This project integrates Micronaut and Server Side View Rendering. JSON renaming map provided at compile time for JS templates, and runtime for Java The nonce should change for each request/response cycle and can be used by It must be preceded by nonce- and wrapped in single quotes, as per the CSP3 spec:. Micronaut HTTP Client. Learn how to use Micronaut low-level HTTP Client. Simplify your code with the declarative HTTP client. Authors: Sergio del Amo Micronaut Version: 2.0.0.RC1

Comments
  • What's the type for name on the database? What's the value of name right before it "makes it" into the database? Have you checked inserting any other value manually – it might be a "presentation" thing only. And what's the value of name when you retrieve it from the database? I think all those questions might lead you to the problem resolution :)
  • @x80486 I am using GORM Data Service with Mysql. Jeff example worked fine. Thanks
  • Thanks for sharing the code. Its good to know another method of parsing the argument.
  • Thanks for considering my answer
  • I don't think this is for micronauts - the def suggests it is groovy which may perhaps have been a micronaut groovy microservice app. I still don't think it is micronaut since ObjectMapper objectMapper = new ObjectMapper(); returns an error about netty and port 8080 being in use when declared in a controller running on port 80 on a java micronaut project -