Thursday, 3 December 2015

XML : Could not marshal in REST with XML format

I have a GET endpoint, it can return JSON and XML formats:

  @RequestMapping(method = RequestMethod.GET, produces = {"application/json", "application/xml" })  @ResponseBody  public Object getV6Subnet(@RequestBody V6SubnetRequest requestBody){     RestResponse<V6SUBNETREC> response = null;     V6SUBNETREC wsSubnet = null;      // do something to set value for wsSubnet     response = RESTfulUtil.buildSuccessResponse(wsSubnet);     return response;  }    

My RESTfulUtil class:

  public class RESTfulUtil {     public synchronized static RestResponse buildSuccessResponse(Object content){        RestResponse response = new RestResponse();        response.setContent(content);        return response;     }      }    

My RestResponse class:

  @XmlAccessorType(XmlAccessType.FIELD)  @XmlRootElement  public class RestResponse<T> extends SimpleRestResponse {     @XmlElement     private  T content;       public T getContent() {        return content;     }       public void setContent(T content) {        this.content = content;     }  }    

And my SimpleRestResponse class:

  public class SimpleRestResponse {     private int code;     private int httpStatusCode;       public int getCode() {         return code;     }       public void setCode(int code) {        this.code = code;     }       @JsonIgnore     public int getHttpStatusCode() {        return httpStatusCode;     }       public void setHttpStatusCode(int httpStatusCode) {        this.httpStatusCode = httpStatusCode;     }  }    

When I request JSON format, I get:

  {"code":21,"content":{"error":"Subnet not found."}}    

But when I request XML format, there's an error:

  Could not marshal [ws.restful.model.RestResponse@56b97914]: null; nested exception is javax.xml.bind.MarshalException    org.springframework.http.converter.HttpMessageNotWritableException: Could not marshal [com.lucent.qip.nb.ws.restful.model.RestResponse@56b97914]: null; nested exception is javax.xml.bind.MarshalException   - with linked exception:  [com.sun.istack.SAXException2: class com.lucent.qip.nb.ws.restful.model.ErrorResponse nor any of its super class is known to this context.  javax.xml.bind.JAXBException: class com.lucent.qip.nb.ws.restful.model.ErrorResponse nor any of its super class is known to this context.]      org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter.writeToResult(Jaxb2RootElementHttpMessageConverter.java:184)      org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.writeInternal(AbstractXmlHttpMessageConverter.java:66)      org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:208)      org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:161)      org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:101)      org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:185)      org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71)      org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126)      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)      org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)      org.springframework.web.servlet.FrameworkServlet.doDelete(FrameworkServlet.java:891)      javax.servlet.http.HttpServlet.service(HttpServlet.java:652)      org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)      javax.servlet.http.HttpServlet.service(HttpServlet.java:727)      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)      org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:151)      org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)      com.lucent.qip.utils.filters.ResponseHeaderFilter.doFilter(ResponseHeaderFilter.java:60)      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)      org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)      org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)      org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)      org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)      org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)      com.lucent.qip.nb.ws.restful.security.AuthenticationTokenProcessingFilter.doFilter(AuthenticationTokenProcessingFilter.java:55)      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)      org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)      org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)      org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)      org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)      org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)      org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)      org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)      org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)    

No comments:

Post a Comment