Skip to content

@BodyOptions Decorator

Thiago Bustamante edited this page Jan 20, 2019 · 3 revisions

@BodyOptions Decorator

If you need to inform any options to the body parser, you can use the @BodyOptions decorator.

You can inform any property accepted by bodyParser

For example:

@Path("async")
class TestService {
   @POST
   @Path("test1")
   @BodyOptions({limit:'100kb'})
   testPost(myData) {
      //...
   }

   @GET
   @Path("test2")
   @BodyOptions({extended:false})
   testPost2(@FormParam("field1")myParam) {
      //...
   }
}

It can be used, for example, to inform the bodyParser that it must handle date types for you:

function dateReviver(key, value) {
    let a;
    if (typeof value === 'string') {
        a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
        if (a) {
            return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
                            +a[5], +a[6]));
        }
    }
    return value;
}

@Path('test')
class MyRestService {
   @POST
   @BodyOptions({reviver: dateReviver})
   myHandler(param) {
      //...
   }
}