step1
- 生成器:
Spring Initializr
- 类型:
maven
- Java:
8
step2
- 勾选
Spring Web
--> 创建
step3
- 将
src
设置为源代码根目录
step4
- 创建
controller
和util
包,并写几个XxxController
LoggerUtil
public class LoggerUtil {
private static final String FQCN = LoggerUtil.class.getName();
public static void info(String message, Object... objects) {
message = format(message, objects);
Throwable throwable = getThrowable(objects);
getLogger().log(null, FQCN, LocationAwareLogger.INFO_INT, message, null, throwable);
}
public static void error(String message, Object... objects) {
message = format(message, objects);
Throwable throwable = getThrowable(objects);
getLogger().log(null, FQCN, LocationAwareLogger.ERROR_INT, message, null, throwable);
}
public static void warn(String message, Object... objects) {
message = format(message, objects);
Throwable throwable = getThrowable(objects);
getLogger().log(null, FQCN, LocationAwareLogger.WARN_INT, message, null, throwable);
}
public static void error(Exception e) {
getLogger().log(null, FQCN, LocationAwareLogger.WARN_INT, e.getMessage(), null, e);
}
public static String format(String message, Object... objects) {
if (objects.length == 0) {
return message;
}
for (int i = 0, length = objects.length; i < length; i++) {
Object o = objects[i];
if (o == null || o instanceof String || o instanceof Throwable) {
continue;
}
if (ClassUtils.isPrimitiveOrWrapper(o.getClass()) || o instanceof BigDecimal) {
objects[i] = o.toString();
continue;
}
// 这里是判断
try {
o.getClass().getDeclaredMethod("toString");
} catch (NoSuchMethodException e) {
ObjectMapper objectMapper = new ObjectMapper();
try {
objects[i] = objectMapper.writeValueAsString(o);
} catch (JsonProcessingException e1) {
LoggerUtil.error(e1);
}
continue;
}
objects[i] = o.toString();
}
message = StringUtils.replace(message, "'", "''");
return MessageFormat.format(message, objects);
}
private static LocationAwareLogger getLogger() {
Class<?> callerClass = getCallerClass(3);
return (LocationAwareLogger) LoggerFactory.getLogger(callerClass.getName());
}
private static Throwable getThrowable(Object... objects) {
for (Object o : objects) {
if (o instanceof Throwable) {
return (Throwable) o;
}
}
return null;
}
private static Class<?> getCallerClass(int depth) {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
String className = stackTrace[depth + 1].getClassName();
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
step5
HelloController
@RestController
@RequestMapping(value = "/hello", method = {RequestMethod.GET, RequestMethod.POST})
public class HelloController {
@RequestMapping(value = "/helloWorld")
public String getHelloWorld() {
LoggerUtil.info("Hello Zjz!");
return "Hello Zjz!";
}
}
//localhost:8080/hello/helloWorld
GetSumController
@RestController
@RequestMapping(value = "/math", method = {RequestMethod.GET, RequestMethod.POST})
public class GetSumController {
@RequestMapping(value = "/sum")
public int getSum(int a,int b) {
LoggerUtil.info("getSum");
return a+b;
}
}
//http://localhost:8080/math/sum?a=1&b=2
GetInfoController
create table tb_person
(
name varchar(20),
age int,
-- 1:男 0:女
gender int
);
insert into tb_person (name, age, gender)
values ('mmz',25,1);
@Slf4j
@RestController
@RequestMapping(value = "/info", method = {RequestMethod.GET, RequestMethod.POST})
public class GetInfoController {
@RequestMapping(value = "/nameInfo")
//获取路径上的参数 @PathVariable("personName")
public String getNameInfo(@PathVariable("personName") String personName) throws ClassNotFoundException {
log.info("获取参数{}:",personName);
LoggerUtil.info("getNameInfo");
Connection conn = null;
Statement stmt = null;
int age = 0;
int gender = -1;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/db1";
String user = "root";
String password = "zjz571588";
conn = DriverManager.getConnection(url, user, password);
// 执行SQL查询
String sql = "SELECT * FROM tb_person where name ='" + personName + "'";
Statement stat = conn.createStatement();
//执行 sql 语句的对象
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) {
age = rs.getInt("age");
gender = rs.getInt("gender");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
return "age=" + age + ", gender=" + gender;
}
}
//http://localhost:8080/info/nameInfo?personName=mmz