Day37(7)-F:\硕士阶段\Java\课程代码\后端\web-ai-code\web-ai-project01\springboot-web-01
HTTP
状态码大全
https://cloud.tencent.com/developer/chapter/13553
package com.itheima;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
public class ResponseController {
/**
* 方法一:设置响应数据
* @param response
* @throws IOException
*/
@RequestMapping("/response")
public void response(HttpServletResponse response) throws IOException {
//1.设置响应状态码
// response.setStatus(HttpServletResponse.SC_OK);
response.setStatus(401);
//2.设置响应头
response.setHeader("name","itheima");
//3.设置响应体
response.getWriter().write("<h1>hello response</h1>");
}
/**
* 方式二:ResponseEntity由Springboot提供
* @return
*/
@RequestMapping("/response2")
public ResponseEntity<String> response2(){
return ResponseEntity.status(401).
header("name","javaweb-ai")
.body("<h1>hello response</h1>");
}
}
package com.itheima.controller;
import cn.hutool.core.io.IoUtil;
import com.itheima.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* 用户信息的controller
*/
@RestController//封装了一个注解@ResponseBody->将controller的返回值直接作为响应体的数据直接响应;如果返回值是对象/集合,会先转接一次变成json
public class userController {
@RequestMapping("/list")
public List<User> list() throws Exception {
//1.加载并读取user.txt文件,获取用户数据
//InputStream in = new FileInputStream(new File("F:\\硕士阶段\\Java\\课程资料\\2025最新版JavaWeb+AI\\资料\\04. 后端Web基础(基础知识)\\资料\\02. 案例资源\\user.txt"));
InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");//导入流
ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>());//通过流读取行,并封装原始数据
//2.解析用户信息,封装为用户对象->list集合
List<User> userlist = lines.stream().map(line -> {//map的逻辑为接收流中的单个元素,返回转换后的元素
//对每一行进行操作
//间隔
String[] parts = line.split(",");
//准备封装到user的数据
Integer id = Integer.parseInt(parts[0]);//转换
String username = parts[1];
String password = parts[2];
String name = parts[3];
Integer age = Integer.parseInt(parts[4]);
LocalDateTime updatetime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return new User(id, username, password, name, age, updatetime);
}).collect(Collectors.toList());//tiList方法也可以,但是这个方法要JDK16之后
//3.返回数据,json格式
return userlist;
}
}
分层解耦
单一职责原则
package com.itheima.controller;
import cn.hutool.core.io.IoUtil;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceimpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* 用户信息的controller
*/
@RestController//封装了一个注解@ResponseBody->将controller的返回值直接作为响应体的数据直接响应;如果返回值是对象/集合,会先转接一次变成json
public class userController {
private UserService userservice = new UserServiceimpl();//面向接口编程
@RequestMapping("/list")//请求之后先到达这里
public List<User> list() throws Exception {
/*
//1.加载并读取user.txt文件,获取用户数据
//InputStream in = new FileInputStream(new File("F:\\硕士阶段\\Java\\课程资料\\2025最新版JavaWeb+AI\\资料\\04. 后端Web基础(基础知识)\\资料\\02. 案例资源\\user.txt"));
InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");//导入流
ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>());//通过流读取行,并封装原始数据
//2.解析用户信息,封装为用户对象->list集合
List<User> userlist = lines.stream().map(line -> {//map的逻辑为接收流中的单个元素,返回转换后的元素
//对每一行进行操作
//间隔
String[] parts = line.split(",");
//准备封装到user的数据
Integer id = Integer.parseInt(parts[0]);//转换
String username = parts[1];
String password = parts[2];
String name = parts[3];
Integer age = Integer.parseInt(parts[4]);
LocalDateTime updatetime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return new User(id, username, password, name, age, updatetime);
}).collect(Collectors.toList());//tiList方法也可以,但是这个方法要JDK16之后
//3.返回数据,json格式
return userlist;
*/
//1.调用service,获取数据
List<User> userlist = userservice.findAll();//调用service处理逻辑板块的方法
//2.返回数据,json格式
return userlist;//由于有RestController注解,将return的数据展示个前端
}
}
package com.itheima.service.impl;
import com.itheima.dao.UserDao;
import com.itheima.dao.impl.UserDaoimpl;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
public class UserServiceimpl implements UserService {
private UserDao userDao = new UserDaoimpl();
@Override
public List<User> findAll() {
//1.调用dao来获取数据
List<String> lines = userDao.findAll();//调用数据源dao的方法
//2.解析用户信息,封装为用户对象->list集合
List<User> userlist = lines.stream().map(line -> {//map的逻辑为接收流中的单个元素,返回转换后的元素
//对每一行进行操作
//间隔
String[] parts = line.split(",");
//准备封装到user的数据
Integer id = Integer.parseInt(parts[0]);//转换
String username = parts[1];
String password = parts[2];
String name = parts[3];
Integer age = Integer.parseInt(parts[4]);
LocalDateTime updatetime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return new User(id, username, password, name, age, updatetime);
}).collect(Collectors.toList());//tiList方法也可以,但是这个方法要JDK16之后
return userlist;//将信息处理完之后给controller
}
}
package com.itheima.service;
import com.itheima.pojo.User;
import java.util.List;
public interface UserService {
public List<User> findAll();
}
package com.itheima.dao.impl;
import cn.hutool.core.io.IoUtil;
import com.itheima.dao.UserDao;
import org.apache.catalina.User;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class UserDaoimpl implements UserDao {
@Override
public List<String> findAll() {
//1.加载并读取user.txt文件,获取用户数据
//InputStream in = new FileInputStream(new File("F:\\硕士阶段\\Java\\课程资料\\2025最新版JavaWeb+AI\\资料\\04. 后端Web基础(基础知识)\\资料\\02. 案例资源\\user.txt"));
InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");//导入流
ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>());//通过流读取行,并封装原始数据
return lines;//将数据打包后返回给service解析
}
}
package com.itheima.dao;
import java.util.List;
public interface UserDao {
/**
* 加载用户数据
* @return
*/
public List<String> findAll();
}
分层解耦
高内聚低耦合
package com.itheima.controller;
import cn.hutool.core.io.IoUtil;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* 用户信息的controller
*/
@RestController//封装了一个注解@ResponseBody->将controller的返回值直接作为响应体的数据直接响应;如果返回值是对象/集合,会先转接一次变成json
public class userController {
@Autowired
private UserService userservice;//面向接口编程
@RequestMapping("/list")//请求之后先到达这里
public List<User> list() throws Exception {
/*
//1.加载并读取user.txt文件,获取用户数据
//InputStream in = new FileInputStream(new File("F:\\硕士阶段\\Java\\课程资料\\2025最新版JavaWeb+AI\\资料\\04. 后端Web基础(基础知识)\\资料\\02. 案例资源\\user.txt"));
InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");//导入流
ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>());//通过流读取行,并封装原始数据
//2.解析用户信息,封装为用户对象->list集合
List<User> userlist = lines.stream().map(line -> {//map的逻辑为接收流中的单个元素,返回转换后的元素
//对每一行进行操作
//间隔
String[] parts = line.split(",");
//准备封装到user的数据
Integer id = Integer.parseInt(parts[0]);//转换
String username = parts[1];
String password = parts[2];
String name = parts[3];
Integer age = Integer.parseInt(parts[4]);
LocalDateTime updatetime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return new User(id, username, password, name, age, updatetime);
}).collect(Collectors.toList());//tiList方法也可以,但是这个方法要JDK16之后
//3.返回数据,json格式
return userlist;
*/
//1.调用service,获取数据
List<User> userlist = userservice.findAll();//调用service处理逻辑板块的方法
//2.返回数据,json格式
return userlist;//由于有RestController注解,将return的数据展示个前端
}
}
package com.itheima.service.impl;
import com.itheima.dao.UserDao;
import com.itheima.dao.impl.UserDaoimpl;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
@Component//将当前类产生的对象交给容器IOC处理
public class UserServiceimpl implements UserService {
@Autowired//应用程序运行时会自动查询该类型(UserDao)的bean对象并赋值给该成员变量
private UserDao userDao;
@Override
public List<User> findAll() {
//1.调用dao来获取数据
List<String> lines = userDao.findAll();//调用数据源dao的方法
//2.解析用户信息,封装为用户对象->list集合
List<User> userlist = lines.stream().map(line -> {//map的逻辑为接收流中的单个元素,返回转换后的元素
//对每一行进行操作
//间隔
String[] parts = line.split(",");
//准备封装到user的数据
Integer id = Integer.parseInt(parts[0]);//转换
String username = parts[1];
String password = parts[2];
String name = parts[3];
Integer age = Integer.parseInt(parts[4]);
LocalDateTime updatetime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return new User(id, username, password, name, age, updatetime);
}).collect(Collectors.toList());//tiList方法也可以,但是这个方法要JDK16之后
return userlist;//将信息处理完之后给controller
}
}
package com.itheima.dao.impl;
import cn.hutool.core.io.IoUtil;
import com.itheima.dao.UserDao;
import org.apache.catalina.User;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@Component//将当前类产生的对象交给容器IOC处理
public class UserDaoimpl implements UserDao {
@Override
public List<String> findAll() {
//1.加载并读取user.txt文件,获取用户数据
//InputStream in = new FileInputStream(new File("F:\\硕士阶段\\Java\\课程资料\\2025最新版JavaWeb+AI\\资料\\04. 后端Web基础(基础知识)\\资料\\02. 案例资源\\user.txt"));
InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");//导入流
ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>());//通过流读取行,并封装原始数据
return lines;//将数据打包后返回给service解析
}
}
bean的默认名字是类名首字母小写
@SpringBootApplication封装了@ComponentScan
是规范并不是规定,都可以被扫描到,只是说区分名字便于区分。
//方式一:属性注入
// @Autowired
// private UserService userservice;//面向接口编程
//方式二:构造器注入
// private final UserService userservice;
//
//
// @Autowired//--------如果当前类中只存在一个构造函数,@Autowired可以省略
// public userController(UserService userservice) {
// this.userservice = userservice;
// }
//方式三:setter方法注入
private UserService userservice;
@Autowired
public void setUserservice(UserService userservice) {
this.userservice = userservice;
}

浙公网安备 33010602011771号