springbootweb案例,出现访问 http://localhost:8080/list 一直处于浏览器运转阶段

让我检查一下修改后的代码是否正确应用,以及是否还有其他问题。

**已查看文件** *UserDaoImpl.java*

我看到代码还没有修改!第 27 行仍然是 `System.in`,这就是问题所在。

**请按照以下步骤操作:**

1. 修改 UserDaoImpl.java

将第 27 行的 `System.in` 改为 `in`:

```java

// ... existing code ...

public class UserDaoImpl implements UserDao {

@Override

public List<String> findAll() {

InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");

// 之前代码自动生成了System.in,会等待控制台输入,如果没有输入,就会一直等待

ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>());

return lines;

}

}

```

**关键修改:第 27 行从 `System.in` 改为 `in`**

2. 同时修改 UserController.java

移除手动创建对象的代码,让 Spring 自动注入:

```java

package com.itheima.controller;

import com.itheima.pojo.User;

import com.itheima.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController

public class UserController {

@Autowired

private UserService userService;

@RequestMapping("/list")

public List<User> list() throws Exception {

List<User> userList = userService.findAll();

return userList;

}

}

```

3. 为 UserServiceImpl 添加 @Service 注解

```java

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.stereotype.Service;

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;

import java.util.List;

import java.util.stream.Collectors;

@Service

public class UserServiceImpl implements UserService {

private UserDao userDao = new UserDaoImpl();

@Override

public List<User> findAll() {

List<String> lines = userDao.findAll();

List<User> userList = lines.stream().map(line -> {

String\[\] parts = line.split(",");

Integer id = Integer.parseInt(parts0);

String username = parts1;

String password = parts2;

String name = parts3;

Integer age = Integer.parseInt(parts4);

LocalDateTime updateTime = LocalDateTime.parse(parts5, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

return new User(id, username, password, name, age, updateTime);

}).collect(Collectors.toList());

return userList;

}

}

```

4. 重启应用

修改完成后:

  1. **停止**当前运行的应用

  2. **重新编译**项目(IDEA 中点击 Build → Rebuild Project)

  3. **重新启动** SpringBootWeb01Application

  4. 访问 `http://localhost:8080/list\`


**问题原因总结:**

  • `System.in` 是标准输入流,会一直等待控制台输入,导致请求阻塞

  • 应该使用 `in`(文件输入流)来读取 user.txt 文件的内容

修改后就能正常访问了!

相关推荐
摇滚侠14 分钟前
SpringBoot 内嵌 TongWeb 东方通替换 Tomcat
java·spring boot·spring
影寂ldy14 分钟前
C#List泛型集合
windows·c#·list
HeLiang717 分钟前
proguard 混淆 使用JDK17 的 springboot4 + JPA
java·spring boot·proguard
武子康18 分钟前
Java-10 深入浅出 MyBatis 一对多与多对多查询配置详解
java·后端
一 乐19 分钟前
网上订餐系统|基于springboot的网上订餐系统设计与实现(源码+数据库+文档)
java·数据库·spring boot·后端·论文·毕设·网上订餐系统
摇滚侠19 分钟前
我把一个依赖安装到了本地仓库,但是IDEA 刷新 maven 提示远程私服仓库找不到,怎么解决
java·maven·intellij-idea
.Cnn24 分钟前
SpringBoot 文件上传与阿里云 OSS 集成
java·spring boot·后端·阿里云
Mininglamp_271826 分钟前
现在入局Agent开发还来得及吗?
java·开发语言
疯狂成瘾者28 分钟前
GHCR 是什么?GitHub 容器镜像仓库技术介绍
java·linux
方也_arkling33 分钟前
【Java-Day10】多态
java·开发语言