• Springboot01-web


    Springboot快速构建

    1. 访问http://start.spring.io

    2. 构建springboot项目,这里选择版本2.0.4

    3. 单击Generate Project按钮下载springboot zip文件

    简单的springmvc web 示例

    • 导入web maven依赖
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    • 编写helloWorldController类
    @RestController
    public class HelloController
    {
        @RequestMapping("/hello")
        public String index()
        {
            return "hello world";
        }
       
    }
    

    note:@RestController是组合注解,相当于spring中的@ResponseMapping + @Controller,标记HelloController为Controller
    纳入spring容器管理,被调用时返回json字符串

    • 启动springboot
      springboot自身集成了tomcat web服务器,所以不需要在给项目配置web服务器
      启动方法1:直接执行xxxApplication.java中的main方法
    @SpringBootApplication
    public class SpringbootDemoApplication
    {
    
        public static void main(String[] args)
        {
            SpringApplication.run(SpringbootDemoApplication.class, args);
            
        }
    }
    

    启动方法2:使用maven将项目打成jar包,在cmd中执行java -jar xxx.jar
    note:

    • 查看@SpringBootAppliation的源码可以看出,它是组合注解,相当于 @Configuration、@EnableAutoConfiguration、@ComponentScan.
    • @Configuration 是一个类级注释,指示对象是一个bean定义的源。@Configuration 类通过 @bean 注解的公共方法声 明bean。@Bean 注释是用来表示一个方法实例化,配置和初始化是由 Spring IoC 容器管理的一个新的对象。通俗 的讲 @Configuration 一般与 @Bean 注解配合使用,用 @Configuration 注解类等价与 XML 中配置 beans,用 @Bean 注解方法等价于 XML 中配置 bean。
    • @EnableAutoConfiguration:启用 Spring 应用程序上下文的自动配置,试图猜测和配置您可能需要的bean。自动配置类通常采用基于你的 classpath 和已经定义的 beans 对象进行应用。被 @EnableAutoConfiguration 注解的类所在的包有特定的意义,并且作为默认配置使用。例如,当扫描 @Entity类的时候它将本使用。通常推荐将 @EnableAutoConfiguration 配置在 root 包下,这样所有的子包、类都可以被查找到。
    • 访问http://localhost:8080/hello,返回页面hello world

    • 编写测试类

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class TestHelloController
    {
        private MockMvc mvc;
    
         @Before
         public void setUp()
         {
             try
            {
                 mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
            } catch (Exception e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
             
         }
    
        @Test
        public void testHello() throws Exception
        {
            System.out.println("----------");
            mvc.perform(MockMvcRequestBuilders.get("/hello")).andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.content().string("hello world"));
            System.out.println("end");
    
        }
    }
    

    note:需要引入三个包

    import static org.hamcrest.Matchers.equalTo;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    

    引用:
    http://blog.didispace.com/spring-boot-learning-1/
    https://blog.csdn.net/claram/article/details/75125749

  • 相关阅读:
    spring整合freemarker 自定义标签
    curl 取不到第二个参数解决方法
    solr5.5教程-solr.home 配置
    solr5.5教程-schema.xml部分配置
    solr5.5教程-solrconfig.xml,加载schema.xml
    solr5.5教程-tomcat布署(2)
    solr5.5教程-tomcat布署
    jsp页面el表达式不起作用
    spring+hibernate--直接修改数据库,再通过hibernate查询数据不变
    13 hbase连接
  • 原文地址:https://www.cnblogs.com/nwu-edu/p/9414050.html
Copyright © 2020-2023  润新知