@ComponentpublicclassSwaggerFilterimplementsFilter{finalStringAPPLICATION_XHTML="application/xhtml";finalStringXML_ELEMENT_START="<Json>";finalStringXML_ELEMENT_END="</Json>";@Overridepublicvoidinit(FilterConfigconfig)throwsServletException{}@OverridepublicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainchain)throwsIOException,ServletException{// TODO Auto-generated method stubHttpServletRequesthttpRequest=(HttpServletRequest)servletRequest;HttpServletResponseresponse=(HttpServletResponse)servletResponse;ByteArrayPrinterpw=newByteArrayPrinter();HttpServletResponsewrappedResp=newHttpServletResponseWrapper(response){@OverridepublicvoidsetContentType(finalStringtype){super.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);}@OverridepublicPrintWritergetWriter(){returnpw.getWriter();}@OverridepublicServletOutputStreamgetOutputStream()throwsIOException{ServletResponseresponse=this.getResponse();Stringct=(response!=null)?response.getContentType():null;if(ct!=null&&ct.contains(APPLICATION_XHTML)){response.setContentType(ct+","+MediaType.APPLICATION_JSON_UTF8_VALUE);}returnpw.getStream();}};chain.doFilter(httpRequest,wrappedResp);byte[]bytes=pw.toByteArray();StringrespBody=newString(bytes);if(respBody.startsWith(XML_ELEMENT_START)){List<String>s13=Stream.of(respBody).filter((s1)->s1.contains(XML_ELEMENT_START)).map((sample)->Arrays.asList(sample.split(" "))).flatMap((listString)->{StringBuffersb=newStringBuffer();listString.forEach(item->{sb.append(item);});returnStream.of(sb.toString().trim().replace(XML_ELEMENT_START,"").replace(XML_ELEMENT_END,""));}).collect(Collectors.toList());Strings14=String.join("",s13);response.getOutputStream().write(s14.getBytes());}else{response.getOutputStream().write(bytes);}}@Overridepublicvoiddestroy(){// TODO Auto-generated method stub}}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.