package com.hyl.controller;
import com.hyl.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("aop")
public class TestController {
@Autowired
private TestService testService;
/**
* 测试正常逻辑请求
* @param name 姓名
* @param age 年龄
* @return 响应结果
*/
@GetMapping("/normal")
public String normalMethod(@RequestParam String name,@RequestParam int age){
return testService.normalMethod(name, age);
}
/**
* 测试可能出现异常的请求
* @param a 参数a
* @param b 参数b
* @return 响应结果
*/
@GetMapping("/exception")
public String exceptionMethod(@RequestParam int a,@RequestParam int b){
try {
int result = testService.exceptionMethod(a, b);
return "请求正常,计算结果:"+result;
}catch (Exception e){
return "请求异常:"+e.getMessage();
}
}
/**
* 测试不带参数的请求
* @return 响应结果
*/
@GetMapping("/noParam")
public String noParamMethod(@RequestParam String str){
return testService.noParamMethod(str);
}
}
TestService。
java复制代码
package com.hyl.service;
public interface TestService {
String normalMethod(String name, int age);
int exceptionMethod(int a, int b);
String noParamMethod(String str);
}
TestServiceImpl。
java复制代码
package com.hyl.service.impl;
import com.hyl.bean.Loggable;
import com.hyl.service.TestService;
import org.springframework.stereotype.Service;
@Service
public class TestServiceImpl implements TestService {
@Loggable(description = "执行正常逻辑", logParameters = true)
@Override
public String normalMethod(String name, int age) {
return "hello "+name+"!age is:"+age;
}
@Loggable(description = "执行可能抛出异常的业务逻辑", logParameters = true)
@Override
public int exceptionMethod(int a, int b) {
if(b==0){
throw new ArithmeticException("除数不能为0");
}
return a/b;
}
@Loggable(description = "执行不记录参数逻辑", logParameters = false)
@Override
public String noParamMethod(String str) {
return "处理了敏感信息:"+ str;
}
}