很好用的request转换为实体方法还有判断实体所有参数不能为空的方法

/// <summary>
/// 模型辅助处理类
/// 2016-04-18
/// </summary>
public class ModelHelper
{
/// <summary>
/// 将数据转化为模型
/// 2016-04-18 基础数据类型:Int32,decimal
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
public static void ConvertToModel<T>(ref T t)
{
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
Type[] tempArr = new Type[] {typeof(Int32),typeof(decimal)};
object tempObject = null;
for (Int32 i = 0; i < properties.Length; i++)
{
if (ReqUtils.FormString(properties[i].Name) != null || properties[i].PropertyType.Name.ToLower() == "httpfilecollection")
{
switch (properties[i].PropertyType.Name.ToLower())
{
case "int32": tempObject = Int32.Parse(string.Format("0{0}", ReqUtils.FormString(properties[i].Name))); break;
case "decimal": tempObject = decimal.Parse(string.Format("0{0}", ReqUtils.FormString(properties[i].Name))); break;
case "httpfilecollection": tempObject = System.Web.HttpContext.Current.Request.Files; break;
default:
tempObject = string.Format("{0}", ReqUtils.FormString(properties[i].Name));
break;
}
properties[i].SetValue(t, tempObject, null);
}
}
}
/// <summary>
/// 该实体是否都为空
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool ModelIsNotNull(object model)
{
bool istrue = true;
Type type = model.GetType();
var flags = GetOnlyProperty();
var list = type.GetProperties(flags).ToList();
foreach (var p in list)
{
if (IsColumn(p))
{
object value = p.GetValue(model, null) ?? "";
if (string.IsNullOrWhiteSpace(value.ToString()))
{
istrue = false;
break;
}
}
}
return istrue;
}
private BindingFlags GetOnlyProperty()
{
return BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance;
}
private bool IsColumn(PropertyInfo info)
{
object[] objs = info.GetCustomAttributes(typeof(Attribute), true);
return objs.Length == 0;
}
/// <summary>
/// 获取操作提示翻译
/// </summary>
/// <param name="key"></param>
/// <param name="lan"></param>
/// <returns></returns>
public static string GetTans(string key, string lan = "")
{
try
{
if (string.IsNullOrWhiteSpace(lan))
{
lan = "cn";
}
if (lan.ToLower().Contains("cn") || lan.ToLower().Contains("zh"))
{
lan = "zh";
}
string className = "PageInfo_" + lan;
var globalResourceObject = HttpContext.GetGlobalResourceObject(className, key);
if (globalResourceObject != null)
return globalResourceObject.ToString() == "" ? key : globalResourceObject.ToString();
else
{
return key;
}
}
catch
{
return key;
}
}
}

转载于:https://www.cnblogs.com/chengleijiang/p/5693861.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以通过使用AOP(面向切面编程)来实现对Spring Boot接口的入参和出参进行打印。 首先,定义一个切面类,在该类中编写打印代码: ``` @Aspect @Component public class LogAspect { private static final Logger LOGGER = LoggerFactory.getLogger(LogAspect.class); @Pointcut("execution(public * com.example.controller.*.*(..))") public void log() {} @Before("log()") public void doBefore(JoinPoint joinPoint) throws Throwable { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); LOGGER.info("请求URL : {}", request.getRequestURL().toString()); LOGGER.info("请求方法 : {}", request.getMethod()); LOGGER.info("请求IP : {}", request.getRemoteAddr()); LOGGER.info("请求入参 : {}", Arrays.toString(joinPoint.getArgs())); } @AfterReturning(returning = "ret", pointcut = "log()") public void doAfterReturning(Object ret) throws Throwable { LOGGER.info("请求出参 : {}", ret); } } ``` 其中,使用@Pointcut注解定义切入点,在本例中为所有在com.example.controller包下的方法。 使用@Before注解定义在切入点之前执行的操作,在本例中为打印请求URL、请求方法、请求IP和请求入参。 使用@AfterReturning注解定义在切入点之后执行的操作,在本例中为打印请求出参。 对于入参中包含MultipartFile类型的情况,您可以使用instanceof进行判断,并对MultipartFile进行特殊处理。例如: ``` for (Object arg : joinPoint.getArgs()) { if (arg instanceof MultipartFile) { ### 回答2: 在Spring Boot中,可以通过一些方式来打印接口的入参和出参。 首先,我们可以在方法参数中使用`@RequestBody`注解来接收请求的入参,该注解可以将请求的JSON数据转换成对应的实体对象。如果实体类中包含了`MultipartFile`类型的属性,可以通过以下代码处理: ```java @PostMapping("/upload") public void uploadFile(@RequestParam("file") MultipartFile file) throws IOException { // 打印入参 System.out.println("入参:"); System.out.println("文件名:" + file.getOriginalFilename()); System.out.println("文件类型:" + file.getContentType()); System.out.println("文件大小:" + file.getSize()); // 具体的文件处理逻辑 // ... } ``` 上述代码中,`@RequestParam("file")`用于获取请求参数中名为`file`的`MultipartFile`类型对象,并打印相关的属性信息。 另外,对于入参是实体类的情况,同样可以通过`@RequestBody`注解来接收请求的JSON数据并转换成对应的实体对象。如果实体类中包含了`MultipartFile`类型的集合属性,可以通过以下代码处理: ```java @PostMapping("/upload") public void uploadFiles(@RequestBody Entity entity) { // 打印入参 System.out.println("入参:"); System.out.println("属性1:" + entity.getProperty1()); System.out.println("属性2:" + entity.getProperty2()); // 打印MultipartFile集合中的属性信息 List<MultipartFile> files = entity.getFiles(); for (MultipartFile file : files) { System.out.println("文件名:" + file.getOriginalFilename()); System.out.println("文件类型:" + file.getContentType()); System.out.println("文件大小:" + file.getSize()); // 具体的文件处理逻辑 // ... } } ``` 上述代码中,`Entity`即为实体类,其中包含了名为`files`的`MultipartFile`集合属性。通过遍历集合中的每个`MultipartFile`对象,可以打印相关的属性信息。 综上所述,通过上述代码可以在Spring Boot中打印接口的入参和出参,包括处理包含`MultipartFile`和实体类的情况。 ### 回答3: 在Spring Boot中打印接口入参和出参可以通过切面编程来实现。首先需要创建一个切面类,用于处理日志的打印。 ```java @Aspect @Component public class LogAspect { private Logger logger = LoggerFactory.getLogger(LogAspect.class); @Pointcut("execution(* com.example.controller.*.*(..))") public void controllerPointcut() {} @Before("controllerPointcut()") public void logRequest(JoinPoint joinPoint) { // 打印接口入参 logger.info("接口入参: {}", Arrays.toString(joinPoint.getArgs())); } @AfterReturning(returning = "result", pointcut = "controllerPointcut()") public void logResponse(Object result) { // 打印接口出参 logger.info("接口出参: {}", result); } } ``` 在切面类中,通过`@Before`注解的方式,在方法执行前打印接口的入参;通过`@AfterReturning`注解的方式,在方法返回后打印接口的出参。 如果接口入参中包含`MultipartFile`,可以在切面类中获取其相关信息并打印出来。 实体类中包含`MultipartFile`集合的情况,可以通过增加一段逻辑来判断并处理打印。例如: ```java if (arg instanceof MultipartFile) { MultipartFile file = (MultipartFile) arg; logger.info("文件名: {}", file.getOriginalFilename()); } ``` 以上代码判断入参是否为`MultipartFile`类型,如果是的话,则打印文件名。 需要注意的是,切面类需要添加`@Aspect`和`@Component`注解,用于声明该类为切面类,并将其纳入Spring容器管理。 另外,需要在Spring Boot的配置类中添加`@EnableAspectJAutoProxy`注解,启用切面的自动代理功能。 以上是一个简单示例,可以根据实际需求进行扩展和调整。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值