spring-boot-ws从.wsdl到?wsdl

spring-boot-starter-web-services 发布的webservice默认url是http://xx:xx/xxx.wsdl,不符合常规的http://xx:xx/xxx?wsdl格式,我们可以利用urlrewritefilter去做一下转发,让一切都回到熟悉的样子。详情如下:

添加依赖

1
2
3
4
5
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>4.0.4</version>
</dependency>

实现UrlRewriteFilter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.tuckey.web.filters.urlrewrite.Conf;
import org.tuckey.web.filters.urlrewrite.UrlRewriteFilter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import java.io.IOException;
/**
* @author fangchao
* @since 2018-08-17 16:01
**/
@Component
public class WsdlUrlRewriteFilter extends UrlRewriteFilter {
private static final String CONFIG_LOCATION = "classpath:/urlrewrite.xml";
@Value(CONFIG_LOCATION)
private Resource resource;
@Override
protected void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
try {
Conf conf = new Conf(filterConfig.getServletContext(), resource.getInputStream(), resource.getFilename(), "");
checkConf(conf);
} catch (IOException ex) {
throw new ServletException("无法读取配置文件: " + CONFIG_LOCATION, ex);
}
}
}

配置转发规则

根据上面的CONFIG_LOCATION指明的位置,新建配置文件urlrewrite.xml,下面是一个例子

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
"http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
<urlrewrite use-query-string="true">
<rule>
<from>/ws/countries[\?]+wsdl</from>
<to>/ws/countries.wsdl</to>
</rule>
</urlrewrite>

这里需要注意的是 rule 默认是正则的,所以遇到特殊字符要转义。

END