不废话Spring Boot(二)Web开发

简介

spring-boot-starter-web为我们在Spring Boot中进行Web开发做了全方位支持,首先其默认集成了Tomcat,并且加入了对Spring MVC的依赖,其次对很多Web开发里的核心内容支持也都很容易掌握。包括像JSON格式输出,配置Filter,配置Log等等

POM中首先要引入spring-boot-starter-web依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Spring Boot的JSON输出

在类上加入@RestController注解即可,那么这里需要注意,Rest风格的Controller只能输出JSON或XML格式,返回页面等形式均不能生效。

package org.dothwinds.springbootstudy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SpringBootStudyApplication {

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

    @GetMapping("/")
    public String sayHello(){
        return "hello world";
    }

}

可以看到返回了JSON格式的输出,如果正常返回页面搭配@Controller使用即可。

自定义Filter

Filter过滤器就不用多说了,用来在处理业务之前做一些预处理,比如权限验证,安全数据过滤等等。在Spring Boot当中定义Filter也比较简单,由于没有配置文件了,所以对应实现都需要写到代码里,常规的实现步骤如下:

  1. 自定义一个Filter
  2. 写一个WebConfiguration,把自定义的Filter注册进来使Filter生效

这里我们使用一种简单的方法,直接定义一个Filter,然后加入注解@WebFilter,可以看一下这个注解的配置,把有用的属性配置一下,如下代码:

package org.dothwinds.springbootstudy.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(filterName = "myFilter", urlPatterns = {"/*"})
public class MyFilter implements Filter {

    private static final Logger LOG = LoggerFactory.getLogger(MyFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        LOG.info("MyFilter init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        LOG.info("this is MyFilter,request url :"+request.getRequestURI());
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        LOG.info("MyFilter destory");
    }
}

启动类里加入@ServletComponentScan,启动的时候扫描Servlet组件

package org.dothwinds.springbootstudy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@ServletComponentScan
@RestController
public class SpringBootStudyApplication {

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

    @GetMapping("/")
    public String sayHello(){
        return "hello world";
    }

}

启动服务,可以看到我们自定义的Filter生效了

访问URL测试一下

日志配置 

 根据项目实际情况,很简单的配置如下:

logging.file.path=X://logs   #本机具体盘符和文件位置,X替换任意盘符
logging.level.org.dothwinds=DEBUG   #配置日志级别
logging.level.org.springframework.web=INFO

然后我们把启动类的请求代码里加入一个debug级别日志打印

package org.dothwinds.springbootstudy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@ServletComponentScan
@RestController
public class SpringBootStudyApplication {

    private static final Logger LOG = LoggerFactory.getLogger(SpringBootStudyApplication.class);

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

    @GetMapping("/")
    public String sayHello(){
        LOG.debug("这是org.dothwinds包下的debug level输出");
        return "hello world";
    }

}

访问一下接口,查看log文件,已经生效了 

官方文档:https://spring.io/projects/spring-boot

代码地址:码云 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值