Java重定向报错:java.lang.IllegalArgumentException: The Unicode character [测] at code point [27,979] ...

目录

    • 一、场景
    • 二、控制器
    • 三、报错信息
    • 四、原因
    • 五、解决


一、场景

控制器重定向时报错


二、控制器

@Slf4j
@RestController
public class RedirectTestController {

	@RequestMapping("/redirectTest")
	public ModelAndView redirectTest() {
		String mainUrl = "redirect:"
                + "https://www.xxx.com.cn/xxxApp/#/Index?id=1&userName=测试1005&workNo=1005&isSystem=0";

		return new ModelAndView(mainUrl);
	}
}

三、报错信息

org.apache.coyote.http11.Http11Processor.prepareResponse The HTTP response header [Location] with value [https://www.xxx.com.cn/xxxApp/#/Index?id=1&userName=测试1005&workNo=1005&isSystem=0] has been removed from the response because it is invalid
java.lang.IllegalArgumentException: The Unicode character [] at code point [27,979] cannot be encoded as it is outside the permitted range of 0 to 255
	at org.apache.tomcat.util.buf.MessageBytes.toBytesSimple(MessageBytes.java:310)
	at org.apache.tomcat.util.buf.MessageBytes.toBytes(MessageBytes.java:283)
	at org.apache.coyote.http11.Http11OutputBuffer.write(Http11OutputBuffer.java:438)
	at org.apache.coyote.http11.Http11OutputBuffer.sendHeader(Http11OutputBuffer.java:417)
	at org.apache.coyote.http11.Http11Processor.prepareResponse(Http11Processor.java:1269)
	at org.apache.coyote.AbstractProcessor.action(AbstractProcessor.java:380)
	at org.apache.coyote.Response.action(Response.java:206)
	at org.apache.coyote.Response.sendHeaders(Response.java:415)
	at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:309)
	at org.apache.catalina.connector.OutputBuffer.close(OutputBuffer.java:268)
	at org.apache.catalina.connector.Response.finishResponse(Response.java:439)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:371)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:617)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:934)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1698)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)

四、原因

url出现没有经过编码的中文


五、解决

使用URLEncoder.encode()对url中文参数进行编码

@RequestMapping("/redirectTest")
public ModelAndView redirectTest() {
	try {
		String mainUrl = "redirect:"
			+ "https://www.xxx.com.cn/xxxApp/#/Index?id=1&userName=" + URLEncoder.encode("测试1005", "UTF-8") + "&workNo=1005&isSystem=0";
            
		return new ModelAndView(mainUrl);
	} catch (UnsupportedEncodingException e) {
		log.error("URL编码出现异常:" + e.getMessage(), e);
		throw new RuntimeException("URL编码出现异常!");
	}
}

你可能感兴趣的:(Java开发问题,java)