系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 脚本中心 > 其它 > 详细页面

SpringBoot服务开启后通过端口访问无反应的解决

时间:2020-10-07来源:www.pcxitongcheng.com作者:电脑系统城

SpringBoot入门Demo,一次深夜踩坑记录。

springboot小项目开启后,访问端口无反应。

首先看我的项目目录:

项目的pom文件内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.bes</groupId>
  <artifactId>spring-colud</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <modules>
    <module>user-service</module>
  </modules>
  <packaging>pom</packaging>
 
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
  </parent>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    <mapper.starter.version>2.0.3</mapper.starter.version>
    <mysql.version>5.1.32</mysql.version>
    <pageHelper.starter.version>1.2.5</pageHelper.starter.version>
  </properties>
 
  <dependencyManagement>
    <dependencies>
      <!-- springcloud -->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
      </dependency>
 
      <!-- 通用Mapper启动器 -->
      <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>${mapper.starter.version}</version>
      </dependency>
 
      <!-- 分页助手启动器 -->
      <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>${pageHelper.starter.version}</version>
      </dependency>
 
      <!-- mysql驱动 -->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
 
  <dependencies>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.4</version>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
 
</project>

我的application.yml配置为:

server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/springboot
username: root
password: root
mybatis:
type-aliases-package: com.bes.user.domain

UserDao为

?
1
2
3
4
5
6
7
8
package com.bes.user.dao;
 
import com.bes.user.domain.User;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
 
public interface UserDao extends Mapper<User> {
}

UserService为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.bes.user.service;
 
import com.bes.user.dao.UserDao;
import com.bes.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
@Service
@Transactional
public class UserService {
  @Autowired
  UserDao userDao;
 
  public User findById(Integer id) {
    User user = userDao.selectByPrimaryKey(id);
    return user;
  }
}

UserController为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.bes.user.web;
 
import com.bes.user.domain.User;
import com.bes.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/user")
public class UserController {
  @Autowired
  UserService userService;
 
  @GetMapping("{id}")
  public User findById(@PathVariable("id")Integer id) {
    User user = userService.findById(id);
    return user;
  }
}

UserApplication为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.bes;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
 
@SpringBootApplication
@MapperScan("com.bes.user.dao")
public class UserApplication {
  public static void main(String[] args) {
    SpringApplication.run(UserApplication.class, args);
  }
}

上述代码是填坑之后的,而错误的原因也非常奇葩在UserService中自动注入UserDao时提示我没有UserDao这个bean.

于是我就在UserDao上加了一个@Repository注解,如下图:

而后UserService不在报错了,运行UserApplication项目正常起来了。

但是通过浏览器访问时却一片空白。

这时在回到IDEA查看下方日志多了两行东西。1111是我调试时让它打印的无关东西。

这个奇怪的错误搞了我几个小时。最后发现不因给在UserDao上加@Reposity注解。UserService中注入Use人Dao报错时应如下处理:

1、鼠标点击报错的UserService中报错的UserDao

2、ALT+ENTER

3、选择第一个选项

4、在选择disable开头的选项

问题解决。

以上这篇SpringBoot服务开启后通过端口访问无反应的解决就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

分享到:

相关信息

  • 如何使用python统计字符在文件中出现的次数

    一、本项目来源: 二、先上传自己写的程序 三、解读程序语句。 四、程序运行效果 五、程序中需要注意的事...

    2023-03-09

  • Go中数组传参的几种方式小结

    本文主要介绍了Go中数组传参的几种方式小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...

    2023-03-09

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载