0

The body of my post request to rest camel component is not in the form of a json. Hence I am not able to extract the key-value pairs. How do I convert body into json? I sent a json object of this type

{
    "filename": "hello.txt",
    "bucketName": "cameltry",
    "accessKey":  "key",
    "secretKey": "key",
    "region" : "us-east-1"
}

I have to use this json data to download a file from AWS S3. The corresponding route in camel is:

public static class HelloRoute extends RouteBuilder {
       
        @Override
        public void configure() {
            rest("/")
                .post("file-from-s3")
                    .route()
                    .setHeader(AWS2S3Constants.KEY, constant($body[filename]))
                    .toD("aws2-s3://${body[bucketName]}?accessKey=${body[accessKey]}&secretKey=${body[secretKey]}&region=${body[region]}&operation=getObject")
                    .to("file:/tmp/")
                    .endRest();
        }

Here $body[filename] turns out to be null as $body is not a json object. How do I convert it into a json object?

Adi shukla
  • 173
  • 3
  • 11

3 Answers3

0

You can do .post("file-from-s3").bindingMode(RestBindingMode.json)

Sneharghya Pathak
  • 990
  • 1
  • 9
  • 19
0

You might consider the org.json library for JSON parsing if the bindingMode(RestBindingMode.json) answer suggested above doesn't work for you.

Source

hjobrien
  • 141
  • 1
  • 10
0

You can use the standard json xstream library, or use one of the other options at https://camel.apache.org/manual/latest/json.html to marshal it into a POJO.

public static class HelloRoute extends RouteBuilder {
   
    @Override
    public void configure() {
        rest("/")
            .post("file-from-s3")
                .route()
                .marshal().json()
                .setHeader(AWS2S3Constants.KEY, constant($body[filename]))
                .toD("aws2-s3://${body[bucketName]}?accessKey=${body[accessKey]}&secretKey=${body[secretKey]}&region=${body[region]}&operation=getObject")
                .to("file:/tmp/")
                .endRest();
    }
pburns1587
  • 108
  • 7