SpringBoot服務(wù)開啟后通過端口訪問無反應(yīng)的解決
SpringBoot入門Demo,一次深夜踩坑記錄。
springboot小項(xiàng)目開啟后,訪問端口無反應(yīng)。
首先看我的項(xiàng)目目錄:

項(xiàng)目的pom文件內(nèi)容如下:
<?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啟動(dòng)器 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mapper.starter.version}</version>
</dependency>
<!-- 分頁(yè)助手啟動(dòng)器 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pageHelper.starter.version}</version>
</dependency>
<!-- mysql驅(qū)動(dòng) -->
<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為
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為:
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為:
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為:
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);
}
}
上述代碼是填坑之后的,而錯(cuò)誤的原因也非常奇葩在UserService中自動(dòng)注入U(xiǎn)serDao時(shí)提示我沒有UserDao這個(gè)bean.

于是我就在UserDao上加了一個(gè)@Repository注解,如下圖:

而后UserService不在報(bào)錯(cuò)了,運(yùn)行UserApplication項(xiàng)目正常起來了。

但是通過瀏覽器訪問時(shí)卻一片空白。

這時(shí)在回到IDEA查看下方日志多了兩行東西。1111是我調(diào)試時(shí)讓它打印的無關(guān)東西。

這個(gè)奇怪的錯(cuò)誤搞了我?guī)讉€(gè)小時(shí)。最后發(fā)現(xiàn)不因給在UserDao上加@Reposity注解。UserService中注入U(xiǎn)se人Dao報(bào)錯(cuò)時(shí)應(yīng)如下處理:
1、鼠標(biāo)點(diǎn)擊報(bào)錯(cuò)的UserService中報(bào)錯(cuò)的UserDao
2、ALT+ENTER
3、選擇第一個(gè)選項(xiàng)
4、在選擇disable開頭的選項(xiàng)
問題解決。
以上這篇SpringBoot服務(wù)開啟后通過端口訪問無反應(yīng)的解決就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot整合SpringSecurity的完整案例詳解
Spring Security是基于Spring生態(tài)圈的,用于提供安全訪問控制解決方案的框架,Spring Security登錄認(rèn)證主要涉及兩個(gè)重要的接口 UserDetailService和UserDetails接口,本文對(duì)Springboot整合SpringSecurity過程給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-01-01
解決Spring調(diào)用Feign報(bào)錯(cuò):java.io.IOException:Incomplete output
這篇文章主要介紹了解決Spring調(diào)用Feign報(bào)錯(cuò):java.io.IOException:Incomplete output stream問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
JavaMail實(shí)現(xiàn)帶附件的郵件發(fā)送
這篇文章主要為大家詳細(xì)介紹了JavaMail實(shí)現(xiàn)帶附件的郵件發(fā)送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
關(guān)于SpringCloud的微服務(wù)結(jié)構(gòu)及微服務(wù)遠(yuǎn)程調(diào)用
Spring Cloud 是一套完整的微服務(wù)解決方案,基于 Spring Boot 框架,準(zhǔn)確的說,它不是一個(gè)框架,而是一個(gè)大的容器,它將市面上較好的微服務(wù)框架集成進(jìn)來,從而簡(jiǎn)化了開發(fā)者的代碼量,需要的朋友可以參考下2023-05-05
LeetCode程序員面試題之無重復(fù)字符的最長(zhǎng)子串
Java計(jì)算無重復(fù)字符的最長(zhǎng)子串是一種常見的字符串處理算法,它的目的是找出一個(gè)字符串中無重復(fù)字符的最長(zhǎng)子串。該算法可以很好地解決一些字符串處理問題,比如尋找字符串中重復(fù)字符的位置,以及計(jì)算字符串中無重復(fù)字符的最長(zhǎng)子串的長(zhǎng)度。2023-02-02

