Spirng Boot学习系列(二)之入门

(1)Spring Boot是为了简化Spring应用的创建、运行、调试、部署等而出现的,使用Spring boot可以做到专注于Spring应用的开发,而无需过多关注XML的配置;
(2)约定优于配置;

一个基于Spring Boot创建的简单web应用demo

IDE:IntelliJ Idea/依赖包管理:Maven
(1)pom.xml文件的配置



    4.0.0

    com.example
    demo
    0.0.1-SNAPSHOT
    jar

    demo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.4.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework
            spring-aspects
            4.3.2.RELEASE
        
        
            com.alibaba
            fastjson
            1.2.15
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

(2)程序入口,Spring Boot可以直接把应用打包成为一个jar/war包,然后这个jar/war包是可以直接启动的,不需要另外配置一个Web Server。

package com.example.demo;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class DemoApplication extends WebMvcConfigurerAdapter {


    @Override
    public void configureMessageConverters(List> converters) {
        super.configureMessageConverters(converters);
        //1.需要定义一个convert转换消息的对象;
        ResultConverter fastJsonHttpMessageConverter = new ResultConverter();
        //2.添加fastJson的配置信息,比如:是否要格式化返回的json数据;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //3处理中文乱码问题
        List fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        //4.在convert中添加配置信息.
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        //5.将convert添加到converters当中.
        converters.add(fastJsonHttpMessageConverter);
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

(3)接口Controller层,RESTful API

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by haishen on 2017/8/29.
 */
@RestController
public class HelloController {

    @RequestMapping(value = "/hello")
    @ResponseBody
    public Object getHello() {
        Map map = new HashMap<>();
        map.put("one", "1");
        map.put("two", "2");
        return map;
    }

    @RequestMapping(value = "/student")
    @ResponseBody
    public Student getStudent() {
        Student student=new Student();
        student.setsId(1);
        student.setsName("hs");
        student.setGender("male");
        student.setCreateTime(new Date());
        return student;
    }
}

你可能感兴趣的:(Spirng Boot学习系列(二)之入门)