Problem solving: org.springframework.util.InvalidMimeTypeException: Invalid mime type "application/xhtml+xml"

Problem scenario

During the operation of the project, some link access will report errors, and the error prompts are as follows:

org.springframework.http.InvalidMediaTypeException: Invalid mime type "application/xhtml+xml": Invalid token character '+' in token "xhtml+xml"
	at org.springframework.http.MediaType.parseMediaType(MediaType.java:534)
	at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
	at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
	at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
	at org.springframework.http.MediaType.parseMediaTypes(MediaType.java:556)
	at org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping.getAcceptedMediaTypes(WelcomePageHandlerMapping.java:93)
	at org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping.getHandlerInternal(WelcomePageHandlerMapping.java:82)
	at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:401)
	at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1231)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1014)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.orm.hibernate5.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:155)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at com.common.filter.PermissionFilter.doFilter(PermissionFilter.java:74)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at pub.servlet.CsrfFilter.doFilter(CsrfFilter.java:55)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at pub.servlet.TokenLoginFilter.doFilter(TokenLoginFilter.java:91)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at pub.servlet.XssFilter.doFilter(XssFilter.java:89)

This article mainly describes the causes and solutions of this kind of situation!

Problem environment

software version
springboot 2.1.8.RELEASE

problem causes

This is actually a pit! ! !
Insert picture description here
Generally, if you encounter this kind of problem and you can't find an idea, single-step debugging is the fastest way to solve it. There is no need to go to Google or Baidu, because everyone’s situation is not necessarily the same, and other people’s blogs may not clarify the circumstances in which the problem occurred. Therefore, solutions that others can do may not be suitable for the current environment! ! !

After many jumps, finally found the reason! It turns out that the project has been set XssFilter, and the sensitive characters in the parameters will be replaced. Others will convert the header, the code is as follows:

public String getHeader(String name) {
    
    
    String value = super.getHeader(xssEncode(name));
    if (value != null) {
    
    
        value = xssEncode(value);
    }

    return value;
}

If a reader encounters a similar problem, it may be that other classes have converted the parameters during the parameter transfer process, causing the problem to occur! ! !

solution

Refactor the code that was converted from the header, without conversion, directly return to the original character!

result

Solve the problem smoothly! ! !

to sum up

If you encounter a problem, there is a local environment, you can step through the debugging first. Do not look for a solution online, because you may not find it! ! !

Ask for praise

If my article is helpful to everyone, you can click like or favorite at the bottom of the article;
if there is a good discussion, you can leave a message;
if you want to continue to view my future articles, you can click Follow
You can scan the following QR code to follow me 'S public account: Fengye Zhixuege, check out my latest share!
Insert picture description here
Bye bye

Guess you like

Origin blog.csdn.net/u013084266/article/details/112463821