4

我正在尝试通过 RestEASY 发送一个 JSON 对象(它使用杰克逊将字符串编码到我的 pojo)并从我的客户端 POST 到我的服务器。

两者都有一个名为 MessageDto 的 pojo。

客户端有杰克逊库(核心、注释、数据绑定所有 2.2)。有了这个,它会从我的 pojo 中生成一个 JSON 字符串。之后我将它发送到我的服务器。

它工作正常。

但前提是没有变音符号或ß...

服务器:

@Path("/rest")
public interface MessageService {
  @POST
  @Path("/add")
  @Consumes(MediaType.APPLICATION_JSON)
  Response add(@Context
  HttpServletRequest servletRequest, MessageDto message);
}
public class MessageServiceImpl implements MessageService {
  @Override
  public Response add(@Context
  HttpServletRequest servletRequest, MessageDto message) {
    System.out.println("MessageServiceImpl: add " + message);
    return Response.status(Status.CREATED).build();
  }
}

客户:

public abstract class MessageSender {
    public static void send(MessageDto m) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            String urlParameters = mapper.writeValueAsString(m);

            System.out.println(urlParameters);

            String request = "http://localhost:8080/MyProject/rest/add";
            URL url = new URL(request);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setInstanceFollowRedirects(false);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("charset", "utf-8");
            connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
            connection.setUseCaches(false);

            DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();

            int response = connection.getResponseCode();
            System.out.println(response);

            wr.close();
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我的 urlParameters 的 Syso(MessageDto 作为 JSON):

{"id":null,"title":"Müsli","receiverIdList":[],"category":"cereals","priority":"HIGH","content":"Müsli Müsli Mjam Mjam Mjam","functionList":[]}

我收到此错误:

13:47:01,670 WARN  [org.jboss.resteasy.core.SynchronousDispatcher] (http--0.0.0.0-8080-4) Failed executing POST /rest/add: org.jboss.resteasy.spi.ReaderException: org.codehaus.jackson.JsonParseException: Invalid UTF-8 start byte 0xfc
at [Source: org.apache.catalina.connector.CoyoteInputStream@65d397; line: 1, column: 23]
   at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:202) [resteasy-jaxrs-2.3.2.Final.jar:]
   at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:124) [resteasy-jaxrs-2.3.2.Final.jar:]
   at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:147) [resteasy-jaxrs-2.3.2.Final.jar:]
   at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257) [resteasy-jaxrs-2.3.2.Final.jar:]
   at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222) [resteasy-jaxrs-2.3.2.Final.jar:]
   at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211) [resteasy-jaxrs-2.3.2.Final.jar:]
   at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:525) [resteasy-jaxrs-2.3.2.Final.jar:]
   at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:502) [resteasy-jaxrs-2.3.2.Final.jar:]
   at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119) [resteasy-jaxrs-2.3.2.Final.jar:]
   at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.2.Final.jar:]
   at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.2.Final.jar:]
   at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.2.Final.jar:]
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
   at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
   at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
   at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_26]
Caused by: org.codehaus.jackson.JsonParseException: Invalid UTF-8 start byte 0xfc
at [Source: org.apache.catalina.connector.CoyoteInputStream@65d397; line: 1, column: 23]
   at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1432) [jackson-core-asl-1.9.2.jar:1.9.2]
   at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:385) [jackson-core-asl-1.9.2.jar:1.9.2]
   at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidInitial(Utf8StreamParser.java:2796) [jackson-core-asl-1.9.2.jar:1.9.2]
   at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidChar(Utf8StreamParser.java:2790) [jackson-core-asl-1.9.2.jar:1.9.2]
   at org.codehaus.jackson.impl.Utf8StreamParser._finishString2(Utf8StreamParser.java:1972) [jackson-core-asl-1.9.2.jar:1.9.2]
   at org.codehaus.jackson.impl.Utf8StreamParser._finishString(Utf8StreamParser.java:1899) [jackson-core-asl-1.9.2.jar:1.9.2]
   at org.codehaus.jackson.impl.Utf8StreamParser.getText(Utf8StreamParser.java:276) [jackson-core-asl-1.9.2.jar:1.9.2]
   at org.codehaus.jackson.map.deser.std.StringDeserializer.deserialize(StringDeserializer.java:26)
   at org.codehaus.jackson.map.deser.std.StringDeserializer.deserialize(StringDeserializer.java:13)
   at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
   at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414)
   at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
   at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
   at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2695)
   at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1308)
   at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
   at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:105) [resteasy-jaxrs-2.3.2.Final.jar:]
   at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.read(GZIPDecodingInterceptor.java:61) [resteasy-jaxrs-2.3.2.Final.jar:]
   at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:108) [resteasy-jaxrs-2.3.2.Final.jar:]
   at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:169) [resteasy-jaxrs-2.3.2.Final.jar:]
   ... 26 more

日志说,有一些无效的 utf-8。但为什么?我两边都用杰克逊...

顺便说一句:没有'ü'它工作正常:

14:02:26,128 INFO  [stdout] (http--0.0.0.0-8080-1) {"id":null,"title":"Musli","receiverIdList":[],"category":"cereals","priority":"HIGH","content":"Musli Musli Mjam Mjam Mjam","functionList":[]}

14:02:26,133 INFO  [stdout] (http--0.0.0.0-8080-4) MessageServiceImpl: add MessageDto [id=null, title=Musli, receiverIdList=[], category=cereals, priority=HIGH, content=Musli Musli Mjam Mjam Mjam, functionList=[]]
4

1 回答 1

6

问题是

 wr.writeBytes(urlParameters);

如果我们阅读 API:

public final void writeBytes(String s) throws IOException

将字符串作为字节序列写入底层输出流。字符串中的每个字符通过丢弃其高八位按顺序写出。如果没有抛出异常,则写入的计数器增加 s 的长度。

您可以使用:

public final void writeUTF(String str) throws IOException

以与机器无关的方式使用修改后的 UTF-8 编码将字符串写入底层输出流。

另一种选择是将字符串转换为字节,urlParameters.getBytes("UTF-8");并直接使用连接给出的 OuputStream 写入它们。

于 2013-05-08T12:35:06.373 回答