Announcement Announcement Module
Collapse
No announcement yet.
Spring Rest WebService Exception Handling issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Rest WebService Exception Handling issue

    Hi All,

    I am trying to figure out how to properly catching exception and throw it into xml representation if exception occur however i have failed to marshal the response right after calling the @ExceptionHandler method. I am using HttpMessageConverter. Below is my code snippet and error log. Would like to know isn't i implement wrongly or is not recommended way of doing it. Thanks.

    1. Controller
    @RequestMapping(method=RequestMethod.POST, value="/sms")
    public @ResponseBody Object addSms(@RequestBody SendSmsAdapterVO sendSmsAdapterVO) {

    throw new Exception(10001,"Service Exception");

    }

    @ExceptionHandler({Exception.class})
    public @ResponseBody ServiceException handleIOException(Exception ex, HttpServletRequest request, HttpServletResponse response) {
    ServiceException se = new ServiceException();
    se.setMessageId(ex.getMessage());

    return se;
    }

    2.Exception
    public class Exception extends RuntimeException{

    int statusCode;
    String message ;

    public Exception(int statusCode, String message){ this.statusCode=statusCode; this.message = message;
    }

    }


    3.ServiceException

    @XmlRootElement(name="ServiceException")
    public class ServiceException {

    private String messageId;
    private String text;

    public String getMessageId() {
    return messageId;
    }
    public void setMessageId(String messageId) {
    this.messageId = messageId;
    }
    public String getText() {
    return text;
    }
    public void setText(String text) {
    this.text = text;
    }

    }


    4.Error log

    org.springframework.web.util.NestedServletExceptio n: Request processing failed; nested exception is com.sla.mobile.lig.sendsmsadapter.web.Exception: Service Exception
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:656)
    org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:560)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    </pre></p><p><b>root cause</b> <pre>com.sla.mobile.lig.sendsmsadapter.web.Excepti on: Service Exception
    com.sla.mobile.lig.sendsmsadapter.web.SendSmsAdapt erController.addSms(SendSmsAdapterController.java: 63)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker.invokeHandlerMethod(HandlerMeth odInvoker.java:176)
    org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.invokeHandlerMethod(An notationMethodHandlerAdapter.java:426)
    org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho dHandlerAdapter.java:414)
    org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:790)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:560)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)



    Regards,

    Cyril
Working...
X