您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關SpringBoot服務開啟后通過端口訪問無反應如何解決,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
項目的pom文件內容如下:
<?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為
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); } }
上述代碼是填坑之后的,而錯誤的原因也非常奇葩在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服務開啟后通過端口訪問無反應如何解決就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。