项目需要从东方通部署迁移到普元PAS部署。记录一下遇到的问题
因为SpringBoot默认使用的容器是tomcat 对应的Websocket实现
PAS中直接使用@ServerEndpoint注解在类上即可启用websocket不需要配置bean ServerEndpointExporter
Springboot项目集成websocket后在国产部署PAS(普元)处理
ws://localhost:8080/websocket-echo/echo (成功)
ws://localhost:8080//websocket-echo/echo (失败)
在tomcat中两种都可以
HTTP RFC 2396 将路径分隔符定义为单斜线.但是,除非您使用某种 URL 重写(在这种情况下重写规则可能会受到斜杠数量的影响),uri 映射到磁盘上的路径,但在(大多数?)现代操作系统中(Linux/Unix, Windows),连续多个路径分隔符没有任何特殊意义,所以/path/to/foo 和/path//tofoo 最终会映射到同一个文件.另外一个可能受到影响的事情是缓存.由于您的浏览器和服务器都会缓存单个页面(根据它们的缓存设置),通过稍微不同的 URI 多次请求相同的文件可能会影响缓存(取决于服务器和客户端的实现).
错误日志 preset Context-Type ‘text/html;charset=UTF-8’
通过和tomcat部署的服务对应,跟踪代码是 contentType返回导致响应类型异常。 正常应该是application/json
引入普元工程中的jar包进行调试最终定位到 (只粘贴了主要逻辑)
public String getHeader(String name) {if (name != null && !name.isEmpty()) {String result = this.handleGetSpecialHeader(name);return result != null ? result : this.headers.getHeader(name);} else {return null;}}private final String handleGetSpecialHeader(String name) {return isSpecialHeader(name) ? this.getValueBasedOnHeader(name) : null;}private static boolean isSpecialHeader(char c) {return c == 'C' || c == 'c' || c == 'U' || c == 'u';}
private String getValueBasedOnHeader(String name) {if (Header.ContentType.toString().equalsIgnoreCase(name)) {String value = this.getContentType();if (value != null) {return value;}} else if (Header.ContentLength.toString().equalsIgnoreCase(name)) {long value = this.getContentLength();if (value >= 0L) {return Long.toString(value);}} else if (Header.Upgrade.toString().equalsIgnoreCase(name)) {return this.getUpgrade();}return null;}
跟踪到这来大概就知道了估计是有过滤器设置了response 的setContentType导致的
查看线程堆找到了对应的过滤器 (处理跨域的) 实际项目部署用不着,删除即可
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");response.setHeader("Access-Control-Allow-Origin", "*");response.setHeader("Access-Control-Allow-Methods", "*");response.setHeader("Access-Control-Max-Age", "3600");response.setHeader("Access-Control-Allow-Headers", "*");response.setHeader("Access-Control-Allow-Credentials", "true");if (HttpMethod.OPTIONS.matches(request.getMethod())) {response.setStatus(HttpStatus.OK.value());return;}filterChain.doFilter(request, response);}
最后对比tomcat的处理
public String getHeader(String name) {return response.getHeader(name);}