首页 > 代码库 > Spring boot 基于Spring MVC的Web应用和REST服务开发

Spring boot 基于Spring MVC的Web应用和REST服务开发

 

Spring Boot利用JavaConfig配置模式以及“约定优于配置”理念,极大简化了基于Spring MVC的Web应用和REST服务开发。

 

  • Servlet:
     1 package org.windwant.spring.servlet;
     2 
     3 import javax.servlet.Servlet;
     4 import javax.servlet.ServletConfig;
     5 import javax.servlet.ServletException;
     6 import javax.servlet.ServletRequest;
     7 import javax.servlet.ServletResponse;
     8 import javax.servlet.annotation.WebServlet;
     9 import java.io.IOException;
    10 
    11 /**
    12  * BootSevlet
    13  */
    14 @WebServlet("/web")
    15 public class BootSevlet implements Servlet {
    16 
    17     @Override
    18     public void init(ServletConfig config) throws ServletException {
    19         System.out.println("servlet init...");
    20     }
    21 
    22     @Override
    23     public ServletConfig getServletConfig() {
    24         return null;
    25     }
    26 
    27     @Override
    28     public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    29         System.out.println(req.getServerName());
    30         res.getWriter().write("got your message!");
    31     }
    32 
    33     @Override
    34     public String getServletInfo() {
    35         return null;
    36     }
    37 
    38     @Override
    39     public void destroy() {
    40         System.out.println("servlet destroy...");
    41     }
    42 }
  • Service:
    1 package org.windwant.spring.service;
    2 
    3 /**
    4  * BootService.
    5  */
    6 public interface BootService {
    7     String hello(String name);
    8 }
     1 package org.windwant.spring.service.impl;
     2 
     3 import org.springframework.stereotype.Service;
     4 import org.windwant.spring.service.BootService;
     5 
     6 /**
     7  * BootServiceImpl
     8  */
     9 @Service
    10 public class BootServiceImpl implements BootService {
    11 
    12     @Override
    13     public String hello(String name){
    14         String message = "Hello " + name + ", welcome to my world!";
    15         System.out.println(message);
    16         return message;
    17     }
    18 }

     

  • Listener:

     1 package org.windwant.spring.listener;
     2 
     3 import org.windwant.spring.BootMgr;
     4 
     5 import javax.servlet.ServletContextEvent;
     6 import javax.servlet.ServletContextListener;
     7 import javax.servlet.annotation.WebListener;
     8 
     9 /**
    10  * BootListener
    11  */
    12 @WebListener
    13 public class BootListener implements ServletContextListener {
    14 
    15     @Override
    16     public void contextInitialized(ServletContextEvent sce) {
    17         System.out.println("listener init... : " + BootMgr.increment());
    18     }
    19 
    20     @Override
    21     public void contextDestroyed(ServletContextEvent sce) {
    22         System.out.println("listener destroy... : " + BootMgr.decrement());
    23     }
    24 }
  • Job:

  •  1 package org.windwant.spring.job;
     2 
     3 import org.springframework.scheduling.annotation.Scheduled;
     4 import org.springframework.stereotype.Component;
     5 
     6 import java.text.SimpleDateFormat;
     7 import java.util.Date;
     8 
     9 /**
    10  * BootJob
    11  */
    12 @Component
    13 public class BootJob {
    14 
    15     private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    16 
    17     @Scheduled(fixedRate = 1000)
    18     public void reportTime(){
    19         System.out.println("current time is: " +  dateFormat.format(new Date()));
    20     }
    21 }
  • Interceptor:

  •  1 package org.windwant.spring.interceptor;
     2 
     3 import org.springframework.web.servlet.HandlerInterceptor;
     4 import org.springframework.web.servlet.ModelAndView;
     5 
     6 import javax.servlet.http.HttpServletRequest;
     7 import javax.servlet.http.HttpServletResponse;
     8 
     9 /**
    10  * BootInterceptor
    11  */
    12 public class BootInterceptor implements HandlerInterceptor {
    13 
    14     @Override
    15     public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
    16         System.out.println("interceptor preHandle...");
    17         return true;
    18     }
    19 
    20     @Override
    21     public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    22         System.out.println("interceptor postHandle...");
    23     }
    24 
    25     @Override
    26     public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    27         System.out.println("interceptor over...");
    28     }
    29 }
     1 package org.windwant.spring.interceptor;
     2 
     3 import org.springframework.context.annotation.Configuration;
     4 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
     5 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
     6 
     7 /**
     8  * BootWebConfig
     9  */
    10 @Configuration
    11 public class BootWebConfig extends WebMvcConfigurerAdapter {
    12 
    13     @Override
    14     public void addInterceptors(InterceptorRegistry registry) {
    15         registry.addInterceptor(new BootInterceptor()).addPathPatterns("/**");
    16         super.addInterceptors(registry);
    17     }
    18 }

     

  • Filter:

  •  1 package org.windwant.spring.filter;
     2 
     3 import javax.servlet.Filter;
     4 import javax.servlet.FilterChain;
     5 import javax.servlet.FilterConfig;
     6 import javax.servlet.ServletException;
     7 import javax.servlet.ServletRequest;
     8 import javax.servlet.ServletResponse;
     9 import javax.servlet.annotation.WebFilter;
    10 import java.io.IOException;
    11 
    12 /**
    13  * BootFilter
    14  */
    15 @WebFilter
    16 public class BootFilter implements Filter {
    17 
    18     @Override
    19     public void init(FilterConfig filterConfig) throws ServletException {
    20         System.out.println("filter init...");
    21     }
    22 
    23     @Override
    24     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    25         System.out.println("filter...");
    26         filterChain.doFilter(servletRequest, servletResponse);
    27     }
    28 
    29     @Override
    30     public void destroy() {
    31         System.out.println("filter destroy...");
    32     }
    33 }
  • BootSpring:

  •  1 package org.windwant.spring;
     2 
     3 import org.joda.time.LocalTime;
     4 import org.springframework.beans.factory.annotation.Autowired;
     5 import org.springframework.boot.SpringApplication;
     6 import org.springframework.boot.autoconfigure.SpringBootApplication;
     7 import org.springframework.boot.web.servlet.ServletComponentScan;
     8 import org.springframework.scheduling.annotation.EnableScheduling;
     9 import org.springframework.web.bind.annotation.PathVariable;
    10 import org.springframework.web.bind.annotation.RequestMapping;
    11 import org.springframework.web.bind.annotation.RestController;
    12 import org.windwant.spring.service.BootService;
    13 
    14 /**
    15  * Hello world!
    16  * @RestController = @Controller + @ResponseBody
    17  * @SpringBootApplication = @Configration +  @EnableAutoConfiguration + @ComponentScan
    18  * @ServletComponentScan scan servlet filter interceptor listener
    19  */
    20 @RestController
    21 @SpringBootApplication
    22 @ServletComponentScan
    23 @EnableScheduling //定时任务
    24 public class BootSpring
    25 {
    26     @RequestMapping("/{name}")
    27     String home(@PathVariable String name){
    28         return bootService.hello(name);
    29     }
    30 
    31     @Autowired
    32     private BootService bootService;
    33 
    34     public static void main( String[] args )
    35     {
    36         LocalTime localTime = new LocalTime();
    37         System.out.println( "Hello World!" + localTime.toString());
    38         localTime = localTime.plusHours(1);
    39         System.out.println("Hello agin World!" + localTime.toString());
    40         SpringApplication.run(BootSpring.class, args);
    41 
    42     }
    43 }
     1 package org.windwant.spring;
     2 
     3 import java.util.concurrent.atomic.AtomicInteger;
     4 
     5 /**
     6  * BootMgr
     7  */
     8 public class BootMgr {
     9 
    10     private static AtomicInteger times = new AtomicInteger();
    11 
    12     public static Integer increment(){
    13         return times.incrementAndGet();
    14     }
    15 
    16     public static Integer decrement(){
    17         return times.decrementAndGet();
    18     }
    19 }

        项目地址:https://github.com/windwant/spring-boot-test

Spring boot 基于Spring MVC的Web应用和REST服务开发