乱码问题

master
pan 5 years ago
parent c90e825bfd
commit fbcba137fa
  1. 1
      .gitignore
  2. 33
      src/main/java/com/example/demo/config/CustomMVCConfiguration.java
  3. 9
      src/main/java/com/example/demo/controller/WebController.java
  4. 9
      src/main/resources/application.yaml
  5. 40
      src/main/resources/logback-spring.xml
  6. 9
      src/test/java/com/example/demo/DemoApplicationTests.java

1
.gitignore vendored

@ -32,3 +32,4 @@ build/
### VS Code ### ### VS Code ###
.vscode/ .vscode/
/src/main/webapp/WEB-INF/classes/ /src/main/webapp/WEB-INF/classes/
/logs/

@ -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<String> responseBodyConverter() {
return new StringHttpMessageConverter(StandardCharsets.UTF_8);
}
@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
converters.add(responseBodyConverter());
}
@Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
}

@ -5,6 +5,8 @@ import com.example.demo.model.*;
import com.example.demo.repository.DiancanDetailRespository; import com.example.demo.repository.DiancanDetailRespository;
import com.example.demo.repository.DiancanRespository; import com.example.demo.repository.DiancanRespository;
import com.example.demo.repository.MenuRepository; import com.example.demo.repository.MenuRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
@ -34,6 +36,8 @@ public class WebController {
@Autowired @Autowired
private DiancanDetailRespository diancanDetailRespository; private DiancanDetailRespository diancanDetailRespository;
private Logger logger= LoggerFactory.getLogger(WebController.class);
@RequestMapping("/index") @RequestMapping("/index")
public String index(Model model,Diancan diancan){ public String index(Model model,Diancan diancan){
List<Diancan> diancans= diancanRespository.findAll((root,query,cb)->{ List<Diancan> diancans= diancanRespository.findAll((root,query,cb)->{
@ -170,8 +174,11 @@ public class WebController {
@GetMapping(path = "/getPinMu",produces = {"application/json;charset=UTF-8"}) @GetMapping(path = "/getPinMu",produces = {"application/json;charset=UTF-8"})
@ResponseBody @ResponseBody
public List<String> getPinMu(Menu menu){ public List<String> getPinMu(Menu menu){
logger.info("根据分类:"+menu.getFenlei()+"查询品目信息");
if( !StringUtils.isEmpty(menu.getFenlei())){ if( !StringUtils.isEmpty(menu.getFenlei())){
return menuRepository.getPinmuByFenlei(menu.getFenlei()); List<String> result= menuRepository.getPinmuByFenlei(menu.getFenlei());
logger.info("根据分类:"+menu.getFenlei()+"查询出"+result.size()+"个品目信息");
return result;
}else{ }else{
return new ArrayList<>(); return new ArrayList<>();
} }

@ -13,4 +13,11 @@ spring:
dialect: org.hibernate.dialect.MySQL8Dialect dialect: org.hibernate.dialect.MySQL8Dialect
format_sql: true format_sql: true
show-sql: true show-sql: true
http:
encoding:
force: true
charset: UTF-8
enabled: true
server:
tomcat:
uri-encoding: UTF-8

@ -0,0 +1,40 @@
<configuration debug="true">
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="APP_NAME" value="Logback"/>
<property name="LOG_HOME_PATH" value="logs"/>
<property name="DEBUG_LOG_FILE" value="${LOG_HOME_PATH}/debug/${APP_NAME}_debug" />
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" />
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${DEBUG_LOG_FILE}.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>60</MaxHistory>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
</appender>
<root level="DEBUG">
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="CONSOLE" />
</root>
</configuration>

@ -4,6 +4,8 @@ import com.example.demo.model.Diancan;
import com.example.demo.repository.DiancanRespository; import com.example.demo.repository.DiancanRespository;
import com.example.demo.repository.MenuRepository; import com.example.demo.repository.MenuRepository;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@ -18,6 +20,8 @@ class DemoApplicationTests {
@Autowired @Autowired
private DiancanRespository diancanRespository; private DiancanRespository diancanRespository;
private Logger logger= LoggerFactory.getLogger(DemoApplicationTests.class);
@Test @Test
void contextLoads() { void contextLoads() {
} }
@ -34,6 +38,11 @@ class DemoApplicationTests {
int a=diancanRespository.countByZhuohao("2"); int a=diancanRespository.countByZhuohao("2");
} }
@Test
void testLog(){
logger.info("ffffffffffffff!");
}
@Test @Test
void testGet(){ void testGet(){
Diancan diancan=diancanRespository.getOne("12"); Diancan diancan=diancanRespository.getOne("12");

Loading…
Cancel
Save