3 Replies Latest reply on Nov 22, 2014 3:37 AM by tungtd

    Return error payload with RESTEasy binding

    tungtd

      Hi,

      I wonder if it is possible to return the error payload with RESTEasy binding. I did try to use Message Composer but it doesn't work that way. When I attempt to set the message content a error object and finally, it ended up throwing a class cast exception: cannot cast the error object to return type object.

      Please give me some ideas!

      Thanks,

        • 1. Re: Return error payload with RESTEasy binding
          mageshbk

          Hi,


          Could you explain what type of errors you are interested in? You could register your own ErrorInterceptor and register it in the providerfactory. But notice that this can only be re-thrown as Exceptions as shown in the example.


          Chapter 43. Client Framework

          • 2. Re: Re: Return error payload with RESTEasy binding
            tungtd

            Hi master,

            I need to return a error message when some exception thrown. Maybe it's some kind of Exception Mapper's result. It doesn't need to work similarly as Exception Mapper but It need to produced the same result.

            <error>
                <errorCode/>
                </errorMessage/>
            </error>
            

            And I also need to change the returned HTTP status code as well.

            Another thing, I tried to change the returned HTTP status code in decompose() method body. But it doesn't work as the quick start example. It seems weird.

            public class RESTCustomMessageComposer extends RESTEasyMessageComposer {
            
                private static Logger logger = Logger
                        .getLogger(RESTCustomMessageComposer.class);
            
                @Override
                public Message compose(RESTEasyBindingData source, Exchange exchange)
                        throws Exception {
                    return super.compose(source, exchange);
                }
            
                @Override
                public RESTEasyBindingData decompose(Exchange exchange,
                        RESTEasyBindingData target) throws Exception {
            //        target = super.decompose(exchange, target);
                    Object content = exchange.getMessage().getContent();
                    logger.info("Changing HTTP Status code...");
                    System.out.println("Changing HTTP Status code...");
                    exchange.getContext().setProperty(RESTEasyContextMapper.HTTP_RESPONSE_STATUS, 541).addLabels(new String[]{EndpointLabel.HTTP.label()});
                    target = super.decompose(exchange, target);
                    return target;
                }
            }
            
            • 3. Re: Return error payload with RESTEasy binding
              tungtd

              Finally, I find out the way to solve this. My REST interface just return a POJO object. So the target's status overriding won't work. I have to change the return type of the REST interface's method to javax.ws.rs.core.Response. And that's it.