我有一个使用underow componentsnet的休息服务。我使用wildfly和wildfly补丁4.7.0(apache camel 2.19)
我有问题要回复我的Rest服务的北欧字母。与邮递员一起测试。
代码是:
@Override
public void configure() throws Exception {
restConfiguration().component("undertow");
rest("/hello").post("/{name}").consumes("application/json").to("direct:testpost");
from("direct:testpost")
.routeId("testpost")
.log("${body}")
.transform().jsonpath("test");
}
当我发送时:
POST / hello / Anton HTTP / 1.1主机:192.168.56.103:8080内容类型: application / json; charset = UTF-8 Cache-Control:no-cache 邮差 - 令牌:c7ed9034-8b58-d104-9804-a1ff60a26f65
{“test”:“用Å测试”}
我收到错误:
2017-06-07 07:06:16,253 INFO [testput](默认任务-5){“test”: “用Å测试}}
2017-06-07 07:06:16,258 ERROR [io.undertow.request](默认任务-5) UT005071:Undertow请求失败HttpServerExchange {POST / hello / Anton请求{Accept = [ / ], 邮差令牌= [a3f9c986-e6d1-1f41-e4f8-54f3e38678c3] 接受语言= [SV-SE,SV; Q = 0.8,的en-US; Q = 0.6,连接; Q = 0.4], Cache-Control = [no-cache],Accept-Encoding = [gzip,deflate], 原点= [铬扩展:// fhbjgbiflinjbdggehcddcbncdddomop] User-Agent = [Mozilla / 5.0(Windows NT 6.1; Win64; x64) AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 59.0.3071.86 Safari / 537.36],Connection = [keep-alive],Content-Length = [24], Content-Type = [application / json;字符集= UTF-8], 主机= [192.168.56.103:8080]}响应{Accept = [ / ], 邮差令牌= [a3f9c986-e6d1-1f41-e4f8-54f3e38678c3] Accept-Language = [sv-SE,sv; q = 0.8,en-US; q = 0.6,en; q = 0.4],name = [Anton], X-Powered-By = [Undertow / 1],Accept-Encoding = [gzip,deflate], breadcrumbId = [ID-本地主机 - 39384-1496833334654-11-5] 服务器= [WildFly / 10], 原点= [铬扩展:// fhbjgbiflinjbdggehcddcbncdddomop] User-Agent = [Mozilla / 5.0(Windows NT 6.1; Win64; x64) AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 59.0.3071.86 Safari / 537.36],Content-Type = [application / json;字符集= UTF-8]}}: org.apache.camel.TypeConversionException:类型转换期间出错 从类型:java.lang.String到所需类型:java.nio.ByteBuffer 使用Å到java.nio.BufferOverflowException进行值测试 at org.apache.camel.impl.converter.BaseTypeConverterRegistry.createTypeConversionException(BaseTypeConverterRegistry.java:629) at org.apache.camel.impl.converter.BaseTypeConverterRegistry.convertTo(BaseTypeConverterRegistry.java:150) at org.apache.camel.impl.converter.BaseTypeConverterRegistry.convertTo(BaseTypeConverterRegistry.java:121) at org.apache.camel.component.undertow.UndertowConsumer.handleRequest(UndertowConsumer.java:135) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202) at io.undertow.server.HttpServerExchange $ 1.run(HttpServerExchange.java:805) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:748)引起:org.apache.camel.RuntimeCamelException: java.nio.BufferOverflowException at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1756) at org.apache.camel.util.ObjectHelper.invokeMethod(ObjectHelper.java:1355) at org.apache.camel.impl.converter.StaticMethodTypeConverter.convertTo(StaticMethodTypeConverter.java:59) at org.apache.camel.impl.converter.BaseTypeConverterRegistry.doConvertTo(BaseTypeConverterRegistry.java:306) at org.apache.camel.impl.converter.BaseTypeConverterRegistry.convertTo(BaseTypeConverterRegistry.java:133) ... 7更多引起:java.nio.BufferOverflowException 在java.nio.HeapByteBuffer.put(HeapByteBuffer.java:189) 在java.nio.ByteBuffer.put(ByteBuffer.java:859) 在org.apache.camel.converter.NIOConverter.toByteBuffer(NIOConverter.java:102) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.camel.util.ObjectHelper.invokeMethod(ObjectHelper.java:1351) ......还有10个
进行配置:
<subsystem xmlns="urn:jboss:domain:undertow:3.1">
<buffer-cache name="default"/>
<server name="default-server">
<http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true"/>
<https-listener name="https" socket-binding="https" security-realm="ApplicationRealm" enable-http2="true"/>
<host name="default-host" alias="localhost">
<location name="/" handler="welcome-content"/>
<filter-ref name="server-header"/>
<filter-ref name="x-powered-by-header"/>
</host>
</server>
<servlet-container name="default">
<jsp-config/>
<websockets/>
</servlet-container>
<handlers>
<file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
</handlers>
<filters>
<response-header name="server-header" header-name="Server" header-value="WildFly/10"/>
<response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/>
</filters>
</subsystem>
所以有任何想法如何解决这个问题?所有帮助表示赞赏!
答案 0 :(得分:0)
这是org.apache.camel.converter.NIOConverter(2.18.4)中的错误
public static ByteBuffer toByteBuffer(String value, Exchange exchange) {
ByteBuffer buf = ByteBuffer.allocate(value.length());
byte[] bytes = null;
if (exchange != null) {
String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
if (charsetName != null) {
try {
bytes = value.getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
LOG.warn("Cannot convert the byte to String with the charset " + charsetName, e);
}
}
}
if (bytes == null) {
bytes = value.getBytes();
}
buf.put(bytes);
buf.flip();
return buf;
}
ByteBuffer使用String.length()进行分配,忽略了它可能包含双字节字符的事实。
我正在通过使用eventlistener替换注册表中的转换器来解决此错误。
void onContextStarting(@Observes CamelContextStartingEvent event) {
def context = event.getContext();
def registry = context.getTypeConverterRegistry();
registry.setTypeConverterExistsLoggingLevel(LoggingLevel.INFO)
registry.setTypeConverterExists(TypeConverterExists.Override)
registry.addTypeConverter(ByteBuffer.class,String.class, new StringToByteBufferConverter());
registry.setTypeConverterExists(TypeConverterExists.Ignore)
}
我替换它的转换器使用byte.length来分配ByteBuffer,因此缓冲区匹配字符串的实际字节数:
import org.apache.camel.Exchange;
import org.apache.camel.support.TypeConverterSupport;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
public class StringToByteBufferConverter extends TypeConverterSupport {
public <T> T convertTo(Class<T> type, Exchange exchange, Object object) {
String value = (String)object;
byte[] bytes = null;
if (exchange != null) {
String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
if (charsetName != null) {
try {
bytes = value.getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
throw new UnsupportedOperationException(e);
}
}
}
if (bytes == null) {
bytes = value.getBytes();
}
ByteBuffer buf = ByteBuffer.allocate(bytes.length);
buf.put(bytes);
buf.flip();
return (T)buf;
}
}
您还可以通过将主体转换为String以外的类型来绕过这个错误来绕过坏转换器:
@Override
public void configure() throws Exception {
restConfiguration().component("undertow");
rest("/hello").post("/{name}").consumes("application/json").to("direct:testpost");
from("direct:testpost")
.routeId("testpost")
.log("${body}")
.transform().jsonpath("test")
.convertBodyTo(byte[].class);
}