SpringBoot WS

时间:2018-04-12 07:32:24

标签: java spring web-services spring-boot wsdl

我使用Spring Boot 2.0.1构建合同第一个Web服务。

我遵循了Spring-boot示例 -

https://spring.io/guides/gs/producing-web-service/

这很好。

我的wsdl现在位于

http://localhost:8080/ws/countries.wsdl 

问题是,作为此Web服务的使用者的应用程序需要将wsdl url写为

http://localhost:8080/ws/countries?wsdl

根据帖子我整合了火鸡UrlRewriteFilter How to use Tuckey urlrewrite in spring boot to access service using ?wsdl

但是,URL重写没有按预期发生。我的urlrewrite.xml看起来像这样。从日志我发现它正在采取xml文件,但没有做url重写。我哪里错了?

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite
        PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
        "http://www.tuckey.org/res/dtds/urlrewrite3.0.dtd">

<urlrewrite>
    <rule>
        <from>/countries?wsdl</from>
        <to>/countries.wsdl</to>
    </rule>
</urlrewrite>

1 个答案:

答案 0 :(得分:4)

我找到了最佳解决方案。如下所示编写自己的过滤器。您可以使用HttpServletRequestWrapper来处理?wsdl扩展并让服务器处理请求。

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;

@Component
public class WSDLQuestionMarkReplaceFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //put init logs
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        if ("wsdl".equalsIgnoreCase(httpRequest.getQueryString())) {
            HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(httpRequest) {
                @Override
                public String getQueryString() {
                    return null;
                }

                @Override
                public String getRequestURI() {
                    return super.getRequestURI() + ".wsdl";
                }
            };
            chain.doFilter(requestWrapper, response);
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
        //put destroy logs
    }
}