简体   繁体   English

Apache CXF拦截器覆盖内容类型

[英]Apache CXF interceptor override content-type

I have a post service that only accepts POST requests with file upload: 我有一个邮政服务,仅接受带有文件上传的POST请求:

@POST
@Path("uploadfile")
@Consumes({ "*/*" })
@Produces({ "*/*" })
public Response uploadFile(@Context UriInfo uri, @Context HttpHeaders httpHeaders, MultipartBody multipartBody);

If a front end client has set "wrong" Content-Type it gets 415 response error. 如果前端客户端已将Content-Type设置为“错误”,则它将收到415响应错误。 With InInterceptor I would like to re-set the message to MultipartBody: 使用InInterceptor,我想将消息重新设置为MultipartBody:

@Override
public void handleMessage(Message message) throws Fault {
    System.out.println(message);
}

Here Message is type org.apache.cxf.message.XMLMessage . 这里的消息是org.apache.cxf.message.XMLMessage类型。 How do I change this that my uploadFile method will accept this request with MultipartBody? 我该如何更改我的uploadFile方法将通过MultipartBody接受此请求?

PS: Front-end clients are unknown and cannot be changed and currently already are sending wrong content-types... PS:前端客户端是未知的,无法更改,当前已经发送了错误的内容类型...

I have tried just changing the Content-type: 我试过只是更改Content-type:

@Override
public void handleMessage(Message message) throws Fault {
    Map<String,String> map = (Map<String, String>)message.get(Message.PROTOCOL_HEADERS);
    message.put(Message.CONTENT_TYPE, "multipart/form-data");
    map.put("content-type", "[multipart/form-data]");
    message.put(Message.PROTOCOL_HEADERS, map);
}

But then I get: 但是然后我得到:

java.lang.RuntimeException: org.apache.cxf.interceptor.Fault: java.lang.String cannot be cast to java.util.List
    at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:116)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:333)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)

Ok, I found out that I had casting wrong... 好吧,我发现我选错了...

Map<String,List> map = (Map<String, List>)message.get(Message.PROTOCOL_HEADERS);

Not

Map<String,String> map = (Map<String, String>)message.get(Message.PROTOCOL_HEADERS);

So now it works... 所以现在可以了...

@Override
public void handleMessage(Message message) throws Fault {
    Map<String,List> map = (Map<String, List>)message.get(Message.PROTOCOL_HEADERS);
    message.put(Message.CONTENT_TYPE, "multipart/form-data");
    map.put("content-type", Collections.singletonList("multipart/form-data"));
    message.put(Message.PROTOCOL_HEADERS, map);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM