【问题标题】:Version control for java restful servletjava restful servlet的版本控制
【发布时间】:2019-04-10 15:57:10
【问题描述】:

我们需要为我们的 API 应用版本控制,当用户向我们的 API 端点(即“http://mycompany/item?version=1”)发送请求时,它会将请求转发到 itemServer_V1.java。

为了实现这个目标,我们将 web.xml 配置如下。

<servlet>
    <servlet-name>item</servlet-name>
    <servlet-class>com.mycompany.Servlet.ItemRequestHandler</servlet-class>

</servlet>
<servlet-mapping>
    <servlet-name>item</servlet-name>
    <url-pattern>/item</url-pattern>
</servlet-mapping>

我们在 MySQL 数据库中创建一个表。

database table

ItemRequestHandler 是一个继承自HttpServlet 的类,它应该根据请求中的version 参数将请求转发给ItemServiceV1 或ItemServiceV2。

我已经完成了 ItemService 类,但我不知道如何将请求从 ItemRequestHandler 转发到 ItemService 类。有人可以告诉我该怎么做吗?

ItemRequestHandler类如下

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException , IOException
{
    String version = req.getParameter("version");
    String fcd = req.getParameter("fcd");
    String client = req.getParameter("client");

    //Find the targetClass from database using the above information.
    targetClass.doGet(req, res); 
}

【问题讨论】:

    标签: java mysql rest servlets


    【解决方案1】:

    我找到了解决办法。

    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        System.out.println("LoginRequestHandler doPost");
        String className = "";
        String version = "";
        String fcd = "login";
        String compid = "";
    
        RequestWrapper currentReq = new RequestWrapper(req);
    
        version = currentReq.getParameter("Version");
        compid = currentReq.getParameter("Compid ");
    
        try {       
            className = findServletByVersion(compid, version, fcd);
    
            Class<?> serviceClass = Class.forName(className);           
            Method method = serviceClass.getDeclaredMethod(MethodName.doPost.toString(), HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(serviceClass.newInstance(), currentReq, res);
    
            return;
        }catch(Exception e) {
            System.out.println(e.toString());
        } catch (DataNotFound e) {
            System.out.println(e.toString());
        }
    }
    

    }

    RequestWrapper的代码

        public class RequestWrapper extends HttpServletRequestWrapper {
    
        private String _body;
    
        public RequestWrapper(HttpServletRequest request) throws IOException {
            super(request);
            _body = "";
            BufferedReader bufferedReader = request.getReader();           
            String line;
            while ((line = bufferedReader.readLine()) != null){
                _body += line;
            }
        }
    
        @Override
        public ServletInputStream getInputStream() throws IOException {
            final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_body.getBytes());
            return new ServletInputStream() {
                public int read() throws IOException {
                    return byteArrayInputStream.read();
                }
            };
        }
    
        @Override
        public BufferedReader getReader() throws IOException {
            return new BufferedReader(new InputStreamReader(this.getInputStream()));
        }
    }
    

    findServletByVersion 的代码

        public String findServletByVersion(String compid, String version, String fcd) throws SQLException, ClassNotFoundException, DataNotFound {
    
        String clsName = "";
        Connection con = null;
        Statement stmt = null;      
        User user = null;
        try {
    
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://YourIpAddress:PortNum/"+schemaName,"account","password");
            String query = "SELECT * FROM "+compid+".restfcd "
                    + "WHERE 1=1 "
                    + "AND compid = '"+compid+"'"
                    + "AND version = '"+version+"'"
                    + "AND fcd = '"+fcd+"'"
                    + "ORDER BY compid desc";
    
            System.out.println(query);
            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);
    
            if(rs!=null) {
                while (rs.next()) {             
                    clsName = rs.getString("fcdcls");            
                }
            }
    
            if(Func.isEmpty(clsName)) {
                throw new DataNotFound("findServletByVersion : no match result!");
            }
    
            return clsName;
    
        } catch (SQLException e) {
            throw new SQLException("findServletByVersion : SQLException!");
        } catch (ClassNotFoundException e) {
            throw new ClassNotFoundException("findServletByVersion : ClassNotFoundException!");
    
        } finally {
            try {
                con.close();
                stmt.close();
            } catch (SQLException sqlee) {
                throw new SQLException("Cannot close conection/statement!");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-21
      • 1970-01-01
      • 2011-12-02
      • 2020-03-27
      • 2021-04-08
      • 2023-03-24
      • 2014-10-29
      • 2010-10-01
      • 1970-01-01
      相关资源
      最近更新 更多