2

我正在实现一个SOAPWeb 服务GAE。由于GAE不支持JAX-WS我选择了这种方式,这基本上是我自己从一个servlet构建soap请求和响应。

一切正常,但我怎样才能实现返回 wsdl 描述http://myurl/MyService?wsdl

我想我必须实现GET我的 servlet 的方法,但是如何实现呢?

4

1 回答 1

2

我附上了一个工作实现。请注意,它基于 Servlet 3.0 规范。如果您使用的是 2.5,注释将不起作用。

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="MyWSServlet", urlPatterns={"/MyService"})
public class MyWSServlet extends HttpServlet {

    private static final long serialVersionUID = 3605874163075522777L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        boolean requestForWSDL = false;
        Enumeration<String> params = req.getParameterNames();
        while (params.hasMoreElements()) {
            if ("wsdl".equalsIgnoreCase(params.nextElement())) {
                requestForWSDL = true;
            }
        }
        if (requestForWSDL) {
            FileInputStream wsdlInputStream = new FileInputStream(req.getServletContext().getRealPath("/wsdl/TemperatureService.wsdl"));
            byte[] buffer = new byte[1024];
            resp.setContentType("application/xml");
            int bytesRead = 0;
            while ((bytesRead = wsdlInputStream.read(buffer)) != -1) {
                resp.getOutputStream().write(buffer, 0, bytesRead);

            }
            wsdlInputStream.close();
            resp.getOutputStream().flush();
        }
    }
}
于 2013-07-18T11:15:58.597 回答