Http入站网关失败非法字符集

时间:2018-05-10 14:28:16

标签: spring http character-encoding gateway inbound

我正在尝试对使用Http入站网关创建的服务编写集成测试,它给了我错误" java.nio.charset.UnsupportedCharsetException:cesu-8"。知道为什么吗?此服务正常运行。我们有一个包应用程序,每天多次使用它。我想要一个可以调用它的集成测试。这是我的测试:

@Test
public void receiveMessage() throws IOException {
    final String xml = testUtil.getTestDocumentAsString("/serviceTestData/document.xml");

    final RestTemplate template = new RestTemplate();
    template.getMessageConverters()
            .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
    final HttpHeaders headers = new HttpHeaders();
    MediaType mediaType = new MediaType("text", "xml", StandardCharsets.UTF_8);
    headers.setContentType(mediaType);

    final String credentials = this.user + ":" + this.pass;
    final byte[] message = credentials.getBytes("UTF-8");
    headers.add("Authorization", "Basic " + DatatypeConverter.printBase64Binary(message));
    final HttpEntity request = new HttpEntity(xml, headers);
    final ResponseEntity<Ack> result = template.postForEntity(url, request, Ack.class);

    System.out.println(result.getBody().getStatus());
}

错误:     ServletException的根本原因。     java.nio.charset.UnsupportedCharsetException:cesu-8         at java.nio.charset.Charset.forName(Charset.java:536)         在org.springframework.http.HttpHeaders.getAcceptCharset(HttpHeaders.java:596)         在org.springframework.integration.http.support.DefaultHttpHeaderMapper.getHttpHeader(DefaultHttpHeaderMapper.java:963)         在org.springframework.integration.http.support.DefaultHttpHeaderMapper.toHeaders(DefaultHttpHeaderMapper.java:460)         在org.springframework.integration.http.support.DefaultHttpHeaderMapper.toHeaders(DefaultHttpHeaderMapper.java:72)         在org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.actualDoHandleRequest(HttpRequestHandlingEndpointSupport.java:459)         在org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.doHandleRequest(HttpRequestHandlingEndpointSupport.java:399)         在org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.handleRequest(HttpRequestHandlingMessagingGateway.java:103)         在org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:51)         在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)         在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)         在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)         在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)         在javax.servlet.http.HttpServlet.service(HttpServlet.java:751)         在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)         在javax.servlet.http.HttpServlet.service(HttpServlet.java:844)         在weblogic.servlet.internal.StubSecurityHelper $ ServletServiceAction.run(StubSecurityHelper.java:280)         在weblogic.servlet.internal.StubSecurityHelper $ ServletServiceAction.run(StubSecurityHelper.java:254)         在weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)         在weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341)         在weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)         在weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)         在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:317)         在org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)         在org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)         在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)         在org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)         在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)         在org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)         在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)         在org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)         在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)         在org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)         在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)         在org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)         在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)         在org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:215)         在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)         在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)         在org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)         在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)         在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)         在org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)         在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)         在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)         在org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)         在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)         在org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)         在org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)         在org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)         在org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)         在weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)         at weblogic.servlet.internal.WebAppServletContext $ ServletInvocationAction.wrapRun(WebAppServletContext.java:3388)         at weblogic.servlet.internal.WebAppServletContext $ ServletInvocationAction.run(WebAppServletContext.java:3354)         在weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)         在weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)         在weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)         在weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2238)         在weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2154)         在weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2132)         在weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1564)         在weblogic.servlet.provider.ContainerSupportProviderImpl $ WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)         在weblogic.work.ExecuteThread.execute(ExecuteThread.java:312)         在weblogic.work.ExecuteThread.run(ExecuteThread.java:264)

1 个答案:

答案 0 :(得分:0)

我不熟悉Java或Spring,但是:

  

错误:ServletException的根本原因。 java.nio.charset.UnsupportedCharsetException:cesu-8

     

at java.nio.charset.Charset.forName(Charset.java:536)

     

at org.springframework.http.HttpHeaders.getAcceptCharset(HttpHeaders.java:596)

     

[...]

     

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)

这表明您的测试请求包含一个HTTP标头,如Accept-Charset: cesu-8,您的服务器无法解释。

您的请求似乎是由RestTemplate类生成的。检查它对标题的作用。特别是搜索Accept-Charset(或AcceptCharset)和cesu-8