RestTemplate MalformedByteSequenceException 3字节UTF-8序列的字节2无效

时间:2013-02-22 13:30:31

标签: java jaxb xerces resttemplate

我正在尝试使用RestTemplate从Web服务加载一个对象,如下所示:

Menu menu = null;
RestTemplate rest = new RestTemplate();
String request = "<?xml version=\"1.0\" ?><MenuSync><Header><VendorName>usablehealth</VendorName></Header></MenuSync>";
menu = rest.postForObject("http://localhost:6788/LRequest.cgi", request, Menu.class);

这会产生异常:

  

[com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:   3字节UTF-8序列的字节2无效。] at   com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:389)     在   com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:579)     在   com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)     在   com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)     在   com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:637)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:717)at at   org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)     在   org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)     在   org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)     在   org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)     在   org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)     在   org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)     在   org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)     在   org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)     在   org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)     在org.mortbay.jetty.Server.handle(Server.java:324)处   org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)     在   org.mortbay.jetty.HttpConnection $ RequestHandler.content(HttpConnection.java:843)     在org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)at   org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)at at   org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)at at   org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)     在   org.mortbay.thread.QueuedThreadPool $ PoolThread.run(QueuedThreadPool.java:488)

看起来很简单,但是从webservice加载的XML不包含UTF-8违规。如果我使用显式JAXB来取消对象,它可以正常工作。

Menu menu = null;
RestTemplate rest = new RestTemplate();
String request = "<?xml version=\"1.0\" ?><MenuSync><Header><VendorName>usablehealth</VendorName></Header></MenuSync>";
String response = rest.postForObject("http://localhost:6788/LRequest.cgi", request, String.class);
try {
  JAXBContext context = JAXBContext.newInstance(Menu.class);
  Unmarshaller um = context.createUnmarshaller();
  um.setEventHandler(new CustomValidationEventHandler());
  menu = (Menu) um.unmarshal(new ByteArrayInputStream(response.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
} catch (JAXBException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

我没有在applicationContext中声明RestTemplate bean,因为marchal / unmarchal实现应该是默认的(JAXB?)。这些解决方案有什么区别?

0 个答案:

没有答案