当前位置: 首页 > news >正文

Day37(7)-F:\硕士阶段\Java\课程代码\后端\web-ai-code\web-ai-project01\springboot-web-01

HTTP

image-20251113150227382

image-20251113150758736

image-20251113150710113

image-20251113150722514

状态码大全

https://cloud.tencent.com/developer/chapter/13553

image-20251113152417580

image-20251113152521588

image-20251113154405911

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>");}
}

image-20251113154540093

image-20251113155043243

image-20251113155236424

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;}
}

image-20251113165907220

分层解耦

image-20251113172022954

单一职责原则

image-20251113172414982

image-20251113174921622

image-20251113175209669

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();@Overridepublic 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 {@Overridepublic 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();
}

image-20251113175455535

分层解耦

高内聚低耦合

image-20251113190437895

image-20251113191631728

image-20251113192825080

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 {@Autowiredprivate 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;@Overridepublic 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 {@Overridepublic 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解析}
}

image-20251113194032892

bean的默认名字是类名首字母小写

image-20251113194849020

image-20251113194957478

@SpringBootApplication封装了@ComponentScan

image-20251113195747618

是规范并不是规定,都可以被扫描到,只是说区分名字便于区分。

image-20251113201230937

 //方式一:属性注入
//    @Autowired
//    private UserService userservice;//面向接口编程//方式二:构造器注入
//    private final UserService userservice;
//
//
//    @Autowired//--------如果当前类中只存在一个构造函数,@Autowired可以省略
//    public userController(UserService userservice) {
//        this.userservice = userservice;
//    }//方式三:setter方法注入private UserService userservice;@Autowiredpublic void setUserservice(UserService userservice) {this.userservice = userservice;}

image-20251113202129275

image-20251113202911704

http://www.zskr.cn/news/48813.html

相关文章:

  • 深度学习实验一之图像特征提取和深度学习训练数据标注 - 实践
  • 题解:ABC232G Modulo Shortest Path
  • 如何在 Mac 上安装 MySQL 8.0.20.dmg(从下载到使用全流程,附安装包)
  • 基于Ai元人文构想的关系图
  • 题解:P10360 [PA 2024] Desant 3
  • 软件项目管理工具推荐|飞书项目 vs Asana vs ClickUp vs Jira
  • 题解:AT_abc232_g [ABC232G] Modulo Shortest Path
  • QF-Lib:用一个库搞定Python量化回测和策略开发
  • 软件工程学习日志2025.11.13
  • 完整教程:数值计算-线性方程组的迭代解法
  • 深入解析:三维旋转矩阵的左乘与右乘
  • HEVC视频扩展免费下载
  • 序列化概念及Jackson注解实现动态JSON响应
  • 2025热门学宠物美容师榜:黑龙江学宠物美容师/宠物美容师培训学校毛孩精致变美秘籍!
  • react-window API完全手册:参数、方法与事件全解析 - 指南
  • IOS抓包------Stream
  • 实用指南:数据库的事务和索引
  • 一键账户接管漏洞分析:XSS与CSRF链式攻击实战
  • Vue 3 完全指南:响应式原理、组合式 API 与实战优化 - 实践
  • 创建你的第一个Java文件
  • Imbalance
  • 2025 年 11 月展厅设计公司权威推荐榜:企业展厅、校史馆、博物馆、多媒体数字及VR线上虚拟展厅设计厂家精选
  • 易路AI人才罗盘:点亮组织内部的人才“星空”,让每一次人才决策都精准有据
  • (八大排序)基数排序
  • 业务用例的四个核心要素 - f
  • 20232322 2025-2026-1 《网络与系统攻防技术》实验五实验报告
  • 网易梦幻事业部游戏测试开发外包面经(一面)
  • 抚州0.5mm镜面铝板无压痕模厂家优选,品质稳定采购无忧
  • v模型按开发阶段分为四阶段:单元测试、集成测试、系统测试验收测试
  • Python迭代器_迭代器对象可迭代对象必须分开场景