Spring 下Web API 接口的发布和客户端访问

1.增加实例化bean的class类

    import java.io.IOException;
    import javax.servlet.GenericServlet;
    import javax.servlet.Servlet;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import org.springframework.web.context.WebApplicationContext;
    import org.springframework.web.context.support.WebApplicationContextUtils;

    public class ServiceInfoProxy extends GenericServlet {

private static final long serialVersionUID = 1L;
private String targetBean;
        private Servlet proxy;
    
        @Override
        public void service(ServletRequest arg0, ServletResponse arg1)
                throws ServletException, IOException {
                proxy.service(arg0, arg1);
        }

        @Override
        public void init() throws ServletException {
            this.targetBean = getServletName();
            getServletBean();
            proxy.init(getServletConfig());
        }
    

        private void getServletBean() {                                                            

 WebApplicationContext wac =             WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

            this.proxy = (Servlet)wac.getBean(targetBean);//get proxyBean
        }
}

2.逻辑类TaskStatusCallback继承HttpServlet,如下:

    public class TaskStatusCallback extends HttpServlet{

        public TaskStatusCallback() {
            super();
        }

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,                 IOException {
System.out.println("get 方式逻辑实现");
        }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,                 IOException {
            System.out.println("post 方式逻辑实现");
        }

      @Override
      public void init(ServletConfig config) throws ServletException {
             System.out.println("~~~START1~~~");
         }
    

  }

    3.spring.xml里面加入以下内容:

        

     4.web.xml下发布接口,如下:

        
                taskStatusCallback
                ly.utils.ServiceInfoProxy
                3
          

          

                taskStatusCallback

                

                /service/taskStatusCallback/*
          

 

最后推荐一个比较成熟的spring cloud 架构,欢迎大家收藏 https://gitee.com/gitsc/pro-cloud

你可能感兴趣的:(WEB,API,JAVA)