javarestfulservlet的版本控制

vzgqcmou  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(277)

当用户向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数据库中创建一个表。
数据库表
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); 
}
dtcbnfnu

dtcbnfnu1#

我找到了解决办法。

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!");
        }
    }
}

相关问题