swagger-api / swagger-core

Examples and server integrations for generating the Swagger API Specification, which enables easy access to your REST API
http://swagger.io
Apache License 2.0
7.37k stars 2.17k forks source link

Swagger for servlets #2285

Open starinfor opened 7 years ago

starinfor commented 7 years ago

Is it mandatory to have one servlet for each endpoint ? In my code, one servlet serves multiple web services based on path parameter. Can swagger api work in this case?

webron commented 7 years ago

Can you share your code?

starinfor commented 7 years ago

Can't share exact code but a similar sample would look like this:

public class Test extends HttpServlet
{ 
    @Override
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
        //init code

    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException
    {
            String pathInfo = req.getPathInfo();

            if (pathInfo != null && !"".equals(pathInfo))
            {
                //based on different values call methods/classes accordingly
                //ex: I want to create api for /Test/Ex/test1 , /Test/Example/test2
                               //and both calls would have different set of query parameters
            }

    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException
    {
        doGet(req, res);
    }

    @Override
    public void destroy()
    {
        //destroy context
    }

    }
starinfor commented 7 years ago

The above servlet serves multiple web services based on value of pathinfo.

webron commented 7 years ago

I don't believe we support that mechanism.