You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Benson Margulies <be...@basistech.com> on 2014/10/22 21:49:52 UTC

A small user error -> a ton of log (JAX-RS)

https://gist.github.com/benson-basis/69ef04e9543fe04ac9f5

Here I have a service that picks up a POST'ed body via Jackson. We use
the provider, rather than just declaring the parameter as InputStream,
to get Swagger to be useful. But the punishment is severe in the event
of an error. Can I do something with exception mapping to cut down the
noise?


@POST
    @Path("/extract")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Descriptions({
            @Description(value = "Extract text from given content",
target = DocTarget.METHOD),
            @Description(value = "Extracted text", target = DocTarget.RETURN),
            @Description(value = WADL_INPUT_DESC_TEXT, target =
DocTarget.REQUEST),
            @Description(value = "Response in JSON, {text: \"sample
text\"}", target = DocTarget.RESPONSE),
            @Description(value = "Text extraction service", target =
DocTarget.RESOURCE)
    })
    @ApiOperation(value = "Extract text from given content", notes =
WADL_INPUT_DESC_TEXT + "<p>" + ADM_RESPONSE_DESC_TEXT)
    @ApiResponses(value = {
            @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST,
message = BAD_REQUEST_FORMAT),
            @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN,
message = NO_PROFILE_DATA)
            })
    public Response extractText(
            @ApiParam(name = "profileId", value =
AnnotationGlossary.PROFILE_ID_DESC) @QueryParam("profileId") String
profileId,
            @ApiParam(name = "debug", value =
AnnotationGlossary.DEBUG_DESC) @QueryParam("debug") boolean debug,
            @ApiParam(name = "data", value =
AnnotationGlossary.REQUEST_DATA_DESC, required = true)
RaasRequestTextData data) {
...
}

Re: A small user error -> a ton of log (JAX-RS)

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi Benson, np, setting a singleton provider is all what is needed if the 
resource is singleton, so a singe factory call would do...

Cheers, Sergey
On 22/10/14 21:30, Benson Margulies wrote:
> Please ignore this, I'm sorry about the noise.
>
> On Wed, Oct 22, 2014 at 4:07 PM, Benson Margulies <be...@basistech.com> wrote:
>> I created an exception mapper, but it seems to have no effect on 3.0.2.
>>
>> I wonder if our use of the API to set up a servlet is wrong ... see
>> the call to setProvider down there. Also, do we really need to put the
>> services in as all three of bean objects, resource classes, and
>> resource providers?
>>
>>   @Override
>>      // Called at startup time to register this web service.
>>      public void loadBus(ServletConfig servletConfig) {
>>          super.loadBus(servletConfig);
>>          Bus bus = getBus();
>>          BusFactory.setDefaultBus(bus);
>>          JAXRSServerFactoryBean fb = new JAXRSServerFactoryBean();
>>
>>          // fixing up the root uri
>>          String restEndpointRoot =
>> StringUtils.isEmpty(conf.servletContainer.restEndpointRoot)
>>                  ? "/" : conf.servletContainer.restEndpointRoot.trim();
>>          if (!restEndpointRoot.startsWith("/")) {
>>              restEndpointRoot = "/" + restEndpointRoot;
>>          }
>>          restEndpointRoot = restEndpointRoot.replaceAll("/*$", "") +
>> "/" + RAAS_RS_VERSION;
>>
>>          fb.setAddress(restEndpointRoot);
>>          // do we really need all three of beans, classes, and providers?
>>          fb.setServiceBeanObjects(raasRsService,
>>                  raasRsPingService,
>>                  raasRsInfoService,
>>                  raasRsTextService,
>>                  raasRsRliService,
>>                  raasRsRblService,
>>                  raasRsRexService,
>>                  raasRsResService);
>>          fb.setResourceClasses(RaasRsService.class,
>>                  RaasRsPingService.class,
>>                  RaasRsInfoService.class,
>>                  RaasRsTextService.class,
>>                  RaasRsRliService.class,
>>                  RaasRsRblService.class,
>>                  RaasRsRexService.class,
>>                  RaasRsResService.class);
>>          fb.setResourceProviders(Arrays.<ResourceProvider>asList(
>>                  new SingletonResourceProvider(new RaasRsService(), true),
>>                  new SingletonResourceProvider(new RaasRsPingService(), true),
>>                  new SingletonResourceProvider(new RaasRsInfoService(), true),
>>                  new SingletonResourceProvider(new RaasRsTextService(), true),
>>                  new SingletonResourceProvider(new RaasRsRliService(), true),
>>                  new SingletonResourceProvider(new RaasRsRblService(), true),
>>                  new SingletonResourceProvider(new RaasRsRexService(), true),
>>                  new SingletonResourceProvider(new RaasRsResService(), true)
>>          ));
>>          // called set, but semantically, 'add'.
>>          fb.setProvider(new
>> JacksonJaxbJsonProvider(JsonUtils.getObjectMapper(),
>> JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS));
>>          // catch json exceptions and map.
>>
>>          fb.setProvider(new JsonExceptionMapper());
>>
>>          raasRsServer = fb.create();
>>
>> On Wed, Oct 22, 2014 at 3:49 PM, Benson Margulies <be...@basistech.com> wrote:
>>> https://gist.github.com/benson-basis/69ef04e9543fe04ac9f5
>>>
>>> Here I have a service that picks up a POST'ed body via Jackson. We use
>>> the provider, rather than just declaring the parameter as InputStream,
>>> to get Swagger to be useful. But the punishment is severe in the event
>>> of an error. Can I do something with exception mapping to cut down the
>>> noise?
>>>
>>>
>>> @POST
>>>      @Path("/extract")
>>>      @Consumes(MediaType.APPLICATION_JSON)
>>>      @Produces(MediaType.APPLICATION_JSON)
>>>      @Descriptions({
>>>              @Description(value = "Extract text from given content",
>>> target = DocTarget.METHOD),
>>>              @Description(value = "Extracted text", target = DocTarget.RETURN),
>>>              @Description(value = WADL_INPUT_DESC_TEXT, target =
>>> DocTarget.REQUEST),
>>>              @Description(value = "Response in JSON, {text: \"sample
>>> text\"}", target = DocTarget.RESPONSE),
>>>              @Description(value = "Text extraction service", target =
>>> DocTarget.RESOURCE)
>>>      })
>>>      @ApiOperation(value = "Extract text from given content", notes =
>>> WADL_INPUT_DESC_TEXT + "<p>" + ADM_RESPONSE_DESC_TEXT)
>>>      @ApiResponses(value = {
>>>              @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST,
>>> message = BAD_REQUEST_FORMAT),
>>>              @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN,
>>> message = NO_PROFILE_DATA)
>>>              })
>>>      public Response extractText(
>>>              @ApiParam(name = "profileId", value =
>>> AnnotationGlossary.PROFILE_ID_DESC) @QueryParam("profileId") String
>>> profileId,
>>>              @ApiParam(name = "debug", value =
>>> AnnotationGlossary.DEBUG_DESC) @QueryParam("debug") boolean debug,
>>>              @ApiParam(name = "data", value =
>>> AnnotationGlossary.REQUEST_DATA_DESC, required = true)
>>> RaasRequestTextData data) {
>>> ...
>>> }


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Re: A small user error -> a ton of log (JAX-RS)

Posted by Benson Margulies <be...@basistech.com>.
Please ignore this, I'm sorry about the noise.

On Wed, Oct 22, 2014 at 4:07 PM, Benson Margulies <be...@basistech.com> wrote:
> I created an exception mapper, but it seems to have no effect on 3.0.2.
>
> I wonder if our use of the API to set up a servlet is wrong ... see
> the call to setProvider down there. Also, do we really need to put the
> services in as all three of bean objects, resource classes, and
> resource providers?
>
>  @Override
>     // Called at startup time to register this web service.
>     public void loadBus(ServletConfig servletConfig) {
>         super.loadBus(servletConfig);
>         Bus bus = getBus();
>         BusFactory.setDefaultBus(bus);
>         JAXRSServerFactoryBean fb = new JAXRSServerFactoryBean();
>
>         // fixing up the root uri
>         String restEndpointRoot =
> StringUtils.isEmpty(conf.servletContainer.restEndpointRoot)
>                 ? "/" : conf.servletContainer.restEndpointRoot.trim();
>         if (!restEndpointRoot.startsWith("/")) {
>             restEndpointRoot = "/" + restEndpointRoot;
>         }
>         restEndpointRoot = restEndpointRoot.replaceAll("/*$", "") +
> "/" + RAAS_RS_VERSION;
>
>         fb.setAddress(restEndpointRoot);
>         // do we really need all three of beans, classes, and providers?
>         fb.setServiceBeanObjects(raasRsService,
>                 raasRsPingService,
>                 raasRsInfoService,
>                 raasRsTextService,
>                 raasRsRliService,
>                 raasRsRblService,
>                 raasRsRexService,
>                 raasRsResService);
>         fb.setResourceClasses(RaasRsService.class,
>                 RaasRsPingService.class,
>                 RaasRsInfoService.class,
>                 RaasRsTextService.class,
>                 RaasRsRliService.class,
>                 RaasRsRblService.class,
>                 RaasRsRexService.class,
>                 RaasRsResService.class);
>         fb.setResourceProviders(Arrays.<ResourceProvider>asList(
>                 new SingletonResourceProvider(new RaasRsService(), true),
>                 new SingletonResourceProvider(new RaasRsPingService(), true),
>                 new SingletonResourceProvider(new RaasRsInfoService(), true),
>                 new SingletonResourceProvider(new RaasRsTextService(), true),
>                 new SingletonResourceProvider(new RaasRsRliService(), true),
>                 new SingletonResourceProvider(new RaasRsRblService(), true),
>                 new SingletonResourceProvider(new RaasRsRexService(), true),
>                 new SingletonResourceProvider(new RaasRsResService(), true)
>         ));
>         // called set, but semantically, 'add'.
>         fb.setProvider(new
> JacksonJaxbJsonProvider(JsonUtils.getObjectMapper(),
> JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS));
>         // catch json exceptions and map.
>
>         fb.setProvider(new JsonExceptionMapper());
>
>         raasRsServer = fb.create();
>
> On Wed, Oct 22, 2014 at 3:49 PM, Benson Margulies <be...@basistech.com> wrote:
>> https://gist.github.com/benson-basis/69ef04e9543fe04ac9f5
>>
>> Here I have a service that picks up a POST'ed body via Jackson. We use
>> the provider, rather than just declaring the parameter as InputStream,
>> to get Swagger to be useful. But the punishment is severe in the event
>> of an error. Can I do something with exception mapping to cut down the
>> noise?
>>
>>
>> @POST
>>     @Path("/extract")
>>     @Consumes(MediaType.APPLICATION_JSON)
>>     @Produces(MediaType.APPLICATION_JSON)
>>     @Descriptions({
>>             @Description(value = "Extract text from given content",
>> target = DocTarget.METHOD),
>>             @Description(value = "Extracted text", target = DocTarget.RETURN),
>>             @Description(value = WADL_INPUT_DESC_TEXT, target =
>> DocTarget.REQUEST),
>>             @Description(value = "Response in JSON, {text: \"sample
>> text\"}", target = DocTarget.RESPONSE),
>>             @Description(value = "Text extraction service", target =
>> DocTarget.RESOURCE)
>>     })
>>     @ApiOperation(value = "Extract text from given content", notes =
>> WADL_INPUT_DESC_TEXT + "<p>" + ADM_RESPONSE_DESC_TEXT)
>>     @ApiResponses(value = {
>>             @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST,
>> message = BAD_REQUEST_FORMAT),
>>             @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN,
>> message = NO_PROFILE_DATA)
>>             })
>>     public Response extractText(
>>             @ApiParam(name = "profileId", value =
>> AnnotationGlossary.PROFILE_ID_DESC) @QueryParam("profileId") String
>> profileId,
>>             @ApiParam(name = "debug", value =
>> AnnotationGlossary.DEBUG_DESC) @QueryParam("debug") boolean debug,
>>             @ApiParam(name = "data", value =
>> AnnotationGlossary.REQUEST_DATA_DESC, required = true)
>> RaasRequestTextData data) {
>> ...
>> }

Re: A small user error -> a ton of log (JAX-RS)

Posted by Benson Margulies <be...@basistech.com>.
I created an exception mapper, but it seems to have no effect on 3.0.2.

I wonder if our use of the API to set up a servlet is wrong ... see
the call to setProvider down there. Also, do we really need to put the
services in as all three of bean objects, resource classes, and
resource providers?

 @Override
    // Called at startup time to register this web service.
    public void loadBus(ServletConfig servletConfig) {
        super.loadBus(servletConfig);
        Bus bus = getBus();
        BusFactory.setDefaultBus(bus);
        JAXRSServerFactoryBean fb = new JAXRSServerFactoryBean();

        // fixing up the root uri
        String restEndpointRoot =
StringUtils.isEmpty(conf.servletContainer.restEndpointRoot)
                ? "/" : conf.servletContainer.restEndpointRoot.trim();
        if (!restEndpointRoot.startsWith("/")) {
            restEndpointRoot = "/" + restEndpointRoot;
        }
        restEndpointRoot = restEndpointRoot.replaceAll("/*$", "") +
"/" + RAAS_RS_VERSION;

        fb.setAddress(restEndpointRoot);
        // do we really need all three of beans, classes, and providers?
        fb.setServiceBeanObjects(raasRsService,
                raasRsPingService,
                raasRsInfoService,
                raasRsTextService,
                raasRsRliService,
                raasRsRblService,
                raasRsRexService,
                raasRsResService);
        fb.setResourceClasses(RaasRsService.class,
                RaasRsPingService.class,
                RaasRsInfoService.class,
                RaasRsTextService.class,
                RaasRsRliService.class,
                RaasRsRblService.class,
                RaasRsRexService.class,
                RaasRsResService.class);
        fb.setResourceProviders(Arrays.<ResourceProvider>asList(
                new SingletonResourceProvider(new RaasRsService(), true),
                new SingletonResourceProvider(new RaasRsPingService(), true),
                new SingletonResourceProvider(new RaasRsInfoService(), true),
                new SingletonResourceProvider(new RaasRsTextService(), true),
                new SingletonResourceProvider(new RaasRsRliService(), true),
                new SingletonResourceProvider(new RaasRsRblService(), true),
                new SingletonResourceProvider(new RaasRsRexService(), true),
                new SingletonResourceProvider(new RaasRsResService(), true)
        ));
        // called set, but semantically, 'add'.
        fb.setProvider(new
JacksonJaxbJsonProvider(JsonUtils.getObjectMapper(),
JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS));
        // catch json exceptions and map.

        fb.setProvider(new JsonExceptionMapper());

        raasRsServer = fb.create();

On Wed, Oct 22, 2014 at 3:49 PM, Benson Margulies <be...@basistech.com> wrote:
> https://gist.github.com/benson-basis/69ef04e9543fe04ac9f5
>
> Here I have a service that picks up a POST'ed body via Jackson. We use
> the provider, rather than just declaring the parameter as InputStream,
> to get Swagger to be useful. But the punishment is severe in the event
> of an error. Can I do something with exception mapping to cut down the
> noise?
>
>
> @POST
>     @Path("/extract")
>     @Consumes(MediaType.APPLICATION_JSON)
>     @Produces(MediaType.APPLICATION_JSON)
>     @Descriptions({
>             @Description(value = "Extract text from given content",
> target = DocTarget.METHOD),
>             @Description(value = "Extracted text", target = DocTarget.RETURN),
>             @Description(value = WADL_INPUT_DESC_TEXT, target =
> DocTarget.REQUEST),
>             @Description(value = "Response in JSON, {text: \"sample
> text\"}", target = DocTarget.RESPONSE),
>             @Description(value = "Text extraction service", target =
> DocTarget.RESOURCE)
>     })
>     @ApiOperation(value = "Extract text from given content", notes =
> WADL_INPUT_DESC_TEXT + "<p>" + ADM_RESPONSE_DESC_TEXT)
>     @ApiResponses(value = {
>             @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST,
> message = BAD_REQUEST_FORMAT),
>             @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN,
> message = NO_PROFILE_DATA)
>             })
>     public Response extractText(
>             @ApiParam(name = "profileId", value =
> AnnotationGlossary.PROFILE_ID_DESC) @QueryParam("profileId") String
> profileId,
>             @ApiParam(name = "debug", value =
> AnnotationGlossary.DEBUG_DESC) @QueryParam("debug") boolean debug,
>             @ApiParam(name = "data", value =
> AnnotationGlossary.REQUEST_DATA_DESC, required = true)
> RaasRequestTextData data) {
> ...
> }