编程语言
首页 > 编程语言> > java-如何通过RestEASY将带有变音符(ü,ö,ä)的JSON发送到我的服务器?

java-如何通过RestEASY将带有变音符(ü,ö,ä)的JSON发送到我的服务器?

作者:互联网

我正在尝试通过RestEASY(使用jackson将String编码到我的pojo)发送JSON对象,并从客户端向我的服务器发送POST.

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

客户端具有杰克逊库(核心,注释,databind全部为2.2).这样,它会从我的pojo中生成一个JSON字符串.之后,我将其发送到我的服务器.

工作正常.

但前提是没有Umlaut或ß…

服务器:

@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(MessageDto为JSON)的Syso:

{"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=[]]

解决方法:

问题是

 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编写它们.

标签:rest,jackson,resteasy,json,java
来源: https://codeday.me/bug/20191030/1971647.html