I've managed some workaround via ReaderInterceptor
@ArrayWrapper
public class ArrayWrapperInterceptor implements ReaderInterceptor {
public ArrayWrapperInterceptor() {
openingCurlyBrace = Pattern.compile("\\A\\s*\\{");
closingCurlyBrace = Pattern.compile("\\}\\s*\\Z");
}
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
InputStream is = context.getInputStream();
String content = "";
while (is.available() != 0) {
byte[] bytes = new byte[is.available()];
is.read(bytes);
content = content + new String(bytes);
}
if (content.length() > 0 && openingCurlyBrace.matcher(content).find() && closingCurlyBrace.matcher(content).find()) {
content = "[" + content + "]";
}
context.setInputStream(new ByteArrayInputStream(content.getBytes()));
return context.proceed();
}
private Pattern openingCurlyBrace;
private Pattern closingCurlyBrace;
}
I defined this annotation
@NameBinding
@Retention(RetentionPolicy.RUNTIME)
public @interface ArrayWrapper {}
and put it in both places (at interceptor and at my POST resource method).
In order to make the interceptor work only with @ArrayWrapper
annotation, I've added register(ArrayWrapperInterceptor.class
) to my application class. Without it Jersey does not aware of it and with @Provider annotation my interceptor is global.
Maybe it's not the best solution but now it looks like it's the only one solution available to me.
Later I'll try to investigate a possibility to use some dynamic object in my resource method (like JSON
object) instead of interceptor use.