[英]Apache CXF interceptor override content-type
我有一個郵政服務,僅接受帶有文件上傳的POST請求:
@POST
@Path("uploadfile")
@Consumes({ "*/*" })
@Produces({ "*/*" })
public Response uploadFile(@Context UriInfo uri, @Context HttpHeaders httpHeaders, MultipartBody multipartBody);
如果前端客戶端已將Content-Type設置為“錯誤”,則它將收到415響應錯誤。 使用InInterceptor,我想將消息重新設置為MultipartBody:
@Override
public void handleMessage(Message message) throws Fault {
System.out.println(message);
}
這里的消息是org.apache.cxf.message.XMLMessage類型。 我該如何更改我的uploadFile
方法將通過MultipartBody接受此請求?
PS:前端客戶端是未知的,無法更改,當前已經發送了錯誤的內容類型...
我試過只是更改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);
}
但是然后我得到:
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)
好吧,我發現我選錯了...
Map<String,List> map = (Map<String, List>)message.get(Message.PROTOCOL_HEADERS);
不
Map<String,String> map = (Map<String, String>)message.get(Message.PROTOCOL_HEADERS);
所以現在可以了...
@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.