parent
0163a2799b
commit
042cbf63dc
@ -0,0 +1,25 @@
|
|||||||
|
package cn.com.connor.bh.bhdemo.demos.web.config;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
|
||||||
|
|
||||||
|
@ControllerAdvice
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||||
|
public ResponseEntity<String> handleHttpMessageNotReadable(HttpMessageNotReadableException ex) {
|
||||||
|
Throwable cause = ex.getCause();
|
||||||
|
if (cause instanceof JsonMappingException) {
|
||||||
|
JsonMappingException jsonMappingException = (JsonMappingException) cause;
|
||||||
|
String errorMessage = "Invalid UTF-8 character encountered: " + jsonMappingException.getMessage();
|
||||||
|
// 记录详细的错误日志
|
||||||
|
System.err.println(errorMessage);
|
||||||
|
return new ResponseEntity<>(errorMessage, HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
return new ResponseEntity<>("Invalid request payload", HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package cn.com.connor.bh.bhdemo.demos.web.config;
|
||||||
|
|
||||||
|
import cn.com.connor.bh.bhdemo.demos.web.entity.MinioPojo;
|
||||||
|
import io.minio.MinioClient;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration()
|
||||||
|
public class MinIOConfig {
|
||||||
|
@Autowired
|
||||||
|
private MinioPojo minioPojo;
|
||||||
|
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MinioClient minioClient(){
|
||||||
|
return MinioClient.builder()
|
||||||
|
.endpoint(minioPojo.getUrl()) //传入url地址
|
||||||
|
//传入用户名和密码
|
||||||
|
.credentials(minioPojo.getAccessKey(), minioPojo.getSecretKey())
|
||||||
|
.build(); //完成MinioClient的初始化
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
//package cn.com.connor.bh.bhdemo.demos.web.config;
|
||||||
|
//
|
||||||
|
//import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
//import org.apache.commons.io.IOUtils;
|
||||||
|
//import org.springframework.http.HttpInputMessage;
|
||||||
|
//import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
|
//import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
|
//
|
||||||
|
//import java.io.IOException;
|
||||||
|
//import java.nio.charset.StandardCharsets;
|
||||||
|
//
|
||||||
|
//public class Utf8SafeMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
|
||||||
|
// private final ObjectMapper objectMapper;
|
||||||
|
//
|
||||||
|
// public Utf8SafeMappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
|
||||||
|
// this.objectMapper = objectMapper;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
|
||||||
|
// byte[] body = IOUtils.toByteArray(inputMessage.getBody());
|
||||||
|
// try {
|
||||||
|
// // 检查是否为有效UTF-8
|
||||||
|
// String content = new String(body, StandardCharsets.UTF_8);
|
||||||
|
// validateUtf8(content);
|
||||||
|
// return objectMapper.readValue(content, clazz);
|
||||||
|
// } catch (IllegalArgumentException e) {
|
||||||
|
// throw new HttpMessageNotReadableException("Invalid UTF-8 encoding in request body", e, inputMessage);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private void validateUtf8(String content) {
|
||||||
|
// if (!StandardCharsets.UTF_8.newEncoder().canEncode(content)) {
|
||||||
|
// throw new IllegalArgumentException("Invalid UTF-8 characters found");
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
@ -0,0 +1,22 @@
|
|||||||
|
//package cn.com.connor.bh.bhdemo.demos.web.config;
|
||||||
|
//
|
||||||
|
//import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
//import org.springframework.context.annotation.Bean;
|
||||||
|
//import org.springframework.context.annotation.Configuration;
|
||||||
|
//import org.springframework.http.converter.HttpMessageConverter;
|
||||||
|
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
//
|
||||||
|
//import java.util.List;
|
||||||
|
//
|
||||||
|
//@Configuration
|
||||||
|
//public class WebConfig implements WebMvcConfigurer {
|
||||||
|
// @Bean
|
||||||
|
// public Utf8SafeMappingJackson2HttpMessageConverter utf8SafeMappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
|
||||||
|
// return new Utf8SafeMappingJackson2HttpMessageConverter(objectMapper);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
||||||
|
// converters.add(utf8SafeMappingJackson2HttpMessageConverter(new ObjectMapper()));
|
||||||
|
// }
|
||||||
|
//}
|
@ -0,0 +1,15 @@
|
|||||||
|
package cn.com.connor.bh.bhdemo.demos.web.entity;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Builder
|
||||||
|
public class MinIOUploadRequestPojo {
|
||||||
|
private String filePath; // 文件在客户端或服务器上的路径
|
||||||
|
private String bucketFileName; // 在MinIO中的目标文件名
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package cn.com.connor.bh.bhdemo.demos.web.entity;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@ConfigurationProperties(prefix = "minio")
|
||||||
|
@Component
|
||||||
|
@Data
|
||||||
|
public class MinioPojo {
|
||||||
|
|
||||||
|
private String url;
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
//桶名
|
||||||
|
private String bucketName;
|
||||||
|
private String accessKey;
|
||||||
|
private String secretKey;
|
||||||
|
}
|
Loading…
Reference in new issue