web项目乱码_02springboot系列:web特性

d5219bd23545f96c36a8cd5567db919d.gif

Web 开发

常用的 json 输出、filters、property、log等

1、Json 接口开发

使用 Spring 开发项目,需要提供 json 接口时需要做哪些配置?

  1. 添加 jackjson 等相关 jar 包

  2. 配置 Spring Controller 扫描

  3. 对接的方法添加 @ResponseBody

package com.example.demo.controller;import com.example.demo.model.Customer;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class CustomerController {        @RequestMapping("/getCustomer")    public Customer getCustomer() {        Customer customer=new Customer();        customer.setUserName("张亮");        customer.setPassWord("13128600812");        return customer;    }}

上一篇有介绍的!

package com.example.demo.model;  public class Customer {    private String userName;    private String passWord;    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public String getPassWord() {        return passWord;    }    public void setPassWord(String passWord) {        this.passWord = passWord;    }}

2、自定义Filter

在项目中常常会使用 filters 用于录调用日志、排除有 XSS 威胁的字符、执行权限验证等, Springboot 自动添加了OrderedCharacterEncodingFilter 和 HiddenHttpMethodFilter,并且可以自定义 Filter

两个步骤:

  1. 实现 Filter 接口,实现 Filter 方法

  2. 添加@Configuration 注解,将自定义Filter加入过滤链

package com.example.demo; import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.servlet.*;import javax.servlet.http.HttpServletRequest;import java.io.IOException;@Configurationpublic class WebConfiguration {    @Bean    public FilterRegistrationBean testFilterRegistration() {        FilterRegistrationBean registration = new FilterRegistrationBean();        registration.setFilter(new MyFilter());        registration.addUrlPatterns("/*");        registration.addInitParameter("paramName", "paramValue");        registration.setName("MyFilter");        registration.setOrder(1);        return registration;    }    public class MyFilter implements Filter {        @Override        public void destroy() {        }        @Override        public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain)                throws IOException, ServletException {            HttpServletRequest request = (HttpServletRequest) srequest;            System.out.println("this is MyFilter,url :"+request.getRequestURI());            filterChain.doFilter(srequest, sresponse);        }        @Override        public void init(FilterConfig arg0) throws ServletException {        }    }}

浏览器访问 http://localhost:8080/getCustomer

3d229fa757c739ba4a14dab9d1139768.png

3、自定义Property

在 Web 开发的过程中,我经常需要自定义一些配置文件,如何使用?配置在

application.properties

com.demo.title=张亮com.demo.phone=13128600812com.demo.description=分享生活和技术

数据模型:

package com.example.demo.model;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class Properties {    @Value("${com.demo.title}")    private String title;    @Value("${com.demo.phone}")    private String phone;    @Value("${com.demo.description}")    private String description;    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getPhone() {        return phone;    }    public void setPhone(String phone) {        this.phone = phone;    }    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description;    }}

请求控制类:

package com.example.demo.controller;import com.example.demo.model.Customer;import com.example.demo.model.Properties;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class CustomerController {    @Autowired    private  Properties properties;    @RequestMapping("/getProperties")    public Properties getProperties() {        return properties;    }}

浏览器访问  http://localhost:8080/getProperties

{"title":"张亮","phone":"13128600812","description":"分享生活和技术"}

若出现乱码情况,可以有以下几种解决方法

将application.properites的文件类型修改为UTF-8的编码类型
#application.properites设置spring-boot 编码格式banner.charset=UTF-8server.tomcat.uri-encoding=UTF-8spring.http.encoding.charset=UTF-8spring.http.encoding.enabled=truespring.http.encoding.force=truespring.messages.encoding=UTF-8
idea中File Encodings的Transparent native-to-ascii conversion为true,File -> Settings -> Editor -> File Encodings 将Properties Files (*.properties)下的Default encoding for properties files设置为UTF-8,将Transparent native-to-ascii conversion前的勾选上

7d837ee2b57289f17e666b2a9f4e5e3a.png

4、log配置

logging.file = logs/demo.loglogging.pattern.console = %d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%nlogging.level.com.favorites = DEBUGlogging.level.org.springframework.web = INFOlogging.level.org.hibernate = DEBUG
  • 会在项目路径下创建文件 logs/demo.log ,也可以配置绝对路径;

  • logging.level 后面可以根据包路径配置不同资源的 log 级别

历史回顾:

[20200229]  01-springboot系列:入门篇

 欢迎加入聊技术

d0e8974559a3c3bf49e0cb764f5f00bc.png

 长按关注公众号

7dbc6d42df8ddd7ef5b9c8a106ba443d.png

作者微信号:13128600812

欢迎加入技术研究,备注:1

73a9b86548b1bd70644356e720f49e86.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值