springBoot学习小结

首先我们来到spring官网spring.io选择project然后选择spring framework然后我们选择spring mvc

这里详细讲解了springboot和以前我们使用传统的Dynamic web工程区别

引入包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>springbootcustomer</groupId>
  <artifactId>com.springbootcustomer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
      </dependency>
      
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.2.1.RELEASE</version>
      </dependency>
      
      <dependency>
          <groupId>org.apache.tomcat.embed</groupId>
          <artifactId>tomcat-embed-jasper</artifactId>
          <version>8.0.53</version>
      </dependency>
      
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.62</version>
      </dependency>
      
  </dependencies>
  
</project>

 

入口:启动类

package com.custom.springboot;

import com.custom.springboot.annotation.SpringApplication;
import com.custom.springboot.annotation.SpringBootCustomApplication;

/**
 * 自定义springboot启动类
 * @author lp
 *
 */
@SpringBootCustomApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
    
}
 

自定义注解

package com.custom.springboot.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * 自定义注解上的注解为元注解
 * 
 * @author lp
 *
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ComponentScan
@Configuration
public @interface SpringBootCustomApplication {

}
 

工作类

package com.custom.springboot.annotation;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import com.custom.springboot.App;

public class SpringApplication {
    
    public static void run(Class<App> application, String[] args) {
        
        //内嵌tomcat
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(80);
        tomcat.addContext("/", ClassLoader.getSystemResource("webapp").getPath());
        
        
        //初始化上下文
        AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
        ac.register(application);
        
        //执行了两次,默认启动会执行一次
//        ac.refresh();
        
        DispatcherServlet servlet = new DispatcherServlet(ac);
//        ServletRegistration.Dynamic registration = servletCxt.addServlet();
//        registration.setLoadOnStartup(1);
//        registration.addMapping("/*");
        
        //servlet扫描
        Wrapper wrapper = tomcat.addServlet("/", "DispatcherServlet", servlet);
        //设置启动优先级
        wrapper.setLoadOnStartup(1);
        //设置mapping扫描包
        wrapper.addMapping("/");
        try {
            tomcat.start();
            tomcat.getServer().await();
            
        } catch (LifecycleException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
 

如果要使用@ResponseBody返回json格式数据,需要配置json序列化

package com.custom.springboot.config;

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**
 * 配置json返回
 * @author lp
 *
 */
@Configuration
@EnableWebMvc
public class AppConfig implements WebMvcConfigurer{

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) 
    {
        converters.add(new FastJsonHttpMessageConverter());
        
    }
}

 

 

测试代码

package com.custom.springboot.business.entity;

public class Cat {
    private int id;
    
    private String name;

    public Cat(int id, String name) {
        this.id = id;
        this.name = name;
    }
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
}
 

package com.custom.springboot.business.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.custom.springboot.business.entity.Cat;

@Controller
@RequestMapping("/test")
public class CatController {
    
    @RequestMapping("/cat")
    @ResponseBody
    public Cat test() {
        return new Cat(1, "Hello Tom");
    }

}
 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值