01-1 SpringBoot 集成SpringMVC

一、SpringBoot 集成SpringMVC

项目结构
user-springmvc
--|java
    --|com.mvc
        --|config
        --|controller
        --|entity
        --|service
    --|MvcApplication
--|resources
    --|static
    --|templates
--|poml.xml
文件说明:
  • 1、pom文件:用于maven配置
  • 2、config文件夹:存放java的配置类
  • 3、controller文件夹:用于存放Controller类
  • 4、entity文件夹:用于存放java实体类
  • 5、service文件夹:用于存放service类
  • 6、statice文件夹:用于存放静态资源数据(js,css,图片文件)
  • 7、templates文件夹:用于存放视图文件(html)

1、pom文件添加

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.4.RELEASE
         
    
        UTF-8
        UTF-8
        1.8
    
    
    4.0.0use-springmvc
    
        
            org.springframework.boot
            spring-boot-starter-web
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
    

2、创建启动类

package com.mvc;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
/**
 * @author brusion
 * @date 2018/9/26
 */
​
@SpringBootApplication
public class MvcApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(MvcApplication.class, args);
    }
}
说明:
  • 注解SpringBootApplication:声明为一个SpringBoot启动类

3、Controller类

package com.mvc.controller;
​
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
​
/**
 * @author brusion
 * @date 2018/9/26
 */
@Controller
public class MvcController {
​
​
    @RequestMapping("/test")
    public String say() {
        return "/index";
    }
​
    @RequestMapping("/data")
    @ResponseBody
    public String getData() {
        return getClass().getName().toString();
    }
}
说明:
  • Controller注解:声明当前类为一个Controller类
  • RequestMapping注解:作用于方法上用于声明请求路径
  • ResponseBody注解:用于声明返回的是String类型的数据不是视图

4、测试:

1、运行启动类

2、访问:http://localhost:8080/data

  • 返回String数据

3、访问:http://localhost:8080/test

  • 返回index视图

你可能感兴趣的:(微服务-spring,boot)