From fbcba137fa26eacbf99d33da5efbaf70eba988f8 Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Wed, 15 Jul 2020 21:21:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B9=B1=E7=A0=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../demo/config/CustomMVCConfiguration.java | 33 +++++++++++++++ .../demo/controller/WebController.java | 9 ++++- src/main/resources/application.yaml | 9 ++++- src/main/resources/logback-spring.xml | 40 +++++++++++++++++++ .../example/demo/DemoApplicationTests.java | 9 +++++ 6 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/example/demo/config/CustomMVCConfiguration.java create mode 100644 src/main/resources/logback-spring.xml diff --git a/.gitignore b/.gitignore index 675b9c7..66f7f51 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ build/ ### VS Code ### .vscode/ /src/main/webapp/WEB-INF/classes/ +/logs/ diff --git a/src/main/java/com/example/demo/config/CustomMVCConfiguration.java b/src/main/java/com/example/demo/config/CustomMVCConfiguration.java new file mode 100644 index 0000000..3e814ac --- /dev/null +++ b/src/main/java/com/example/demo/config/CustomMVCConfiguration.java @@ -0,0 +1,33 @@ +package com.example.demo.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import java.nio.charset.StandardCharsets; +import java.util.List; + +@Configuration +public class CustomMVCConfiguration implements WebMvcConfigurer { + + + @Bean + public HttpMessageConverter responseBodyConverter() { + return new StringHttpMessageConverter(StandardCharsets.UTF_8); + } + + @Override + public void configureMessageConverters( + List> converters) { + converters.add(responseBodyConverter()); + } + + @Override + public void configureContentNegotiation( + ContentNegotiationConfigurer configurer) { + configurer.favorPathExtension(false); + } +} diff --git a/src/main/java/com/example/demo/controller/WebController.java b/src/main/java/com/example/demo/controller/WebController.java index 34f047c..db01600 100644 --- a/src/main/java/com/example/demo/controller/WebController.java +++ b/src/main/java/com/example/demo/controller/WebController.java @@ -5,6 +5,8 @@ import com.example.demo.model.*; import com.example.demo.repository.DiancanDetailRespository; import com.example.demo.repository.DiancanRespository; import com.example.demo.repository.MenuRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -34,6 +36,8 @@ public class WebController { @Autowired private DiancanDetailRespository diancanDetailRespository; + private Logger logger= LoggerFactory.getLogger(WebController.class); + @RequestMapping("/index") public String index(Model model,Diancan diancan){ List diancans= diancanRespository.findAll((root,query,cb)->{ @@ -170,8 +174,11 @@ public class WebController { @GetMapping(path = "/getPinMu",produces = {"application/json;charset=UTF-8"}) @ResponseBody public List getPinMu(Menu menu){ + logger.info("根据分类:"+menu.getFenlei()+"查询品目信息"); if( !StringUtils.isEmpty(menu.getFenlei())){ - return menuRepository.getPinmuByFenlei(menu.getFenlei()); + List result= menuRepository.getPinmuByFenlei(menu.getFenlei()); + logger.info("根据分类:"+menu.getFenlei()+"查询出"+result.size()+"个品目信息"); + return result; }else{ return new ArrayList<>(); } diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index a9579ae..8e6be6a 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -13,4 +13,11 @@ spring: dialect: org.hibernate.dialect.MySQL8Dialect format_sql: true show-sql: true - + http: + encoding: + force: true + charset: UTF-8 + enabled: true +server: + tomcat: + uri-encoding: UTF-8 diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..09b2c5d --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + ${FILE_LOG_PATTERN} + + + + ${DEBUG_LOG_FILE}.%d{yyyy-MM-dd}.log + 60 + + + + DEBUG + + + + + + ${FILE_LOG_PATTERN} + + + + INFO + + + + + + + + diff --git a/src/test/java/com/example/demo/DemoApplicationTests.java b/src/test/java/com/example/demo/DemoApplicationTests.java index 16fa7ea..713364f 100644 --- a/src/test/java/com/example/demo/DemoApplicationTests.java +++ b/src/test/java/com/example/demo/DemoApplicationTests.java @@ -4,6 +4,8 @@ import com.example.demo.model.Diancan; import com.example.demo.repository.DiancanRespository; import com.example.demo.repository.MenuRepository; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -18,6 +20,8 @@ class DemoApplicationTests { @Autowired private DiancanRespository diancanRespository; + private Logger logger= LoggerFactory.getLogger(DemoApplicationTests.class); + @Test void contextLoads() { } @@ -34,6 +38,11 @@ class DemoApplicationTests { int a=diancanRespository.countByZhuohao("2"); } + @Test + void testLog(){ + logger.info("ffffffffffffff!"); + } + @Test void testGet(){ Diancan diancan=diancanRespository.getOne("12");