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

网友投稿 1619 2022-11-18


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

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

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

首先看我的项目目录:

项目的pom文件内容如下:

xmlns:xsi="http://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">

4.0.0

com.bes

spring-colud

1.0.0-SNAPSHOT

user-service

pom

org.springframework.boot

spring-boot-starter-parent

2.0.4.RELEASE

UTF-8

UTF-8

1.8

Finchley.SR1

2.0.3

5.1.32

1.2.5

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

tk.mybatis

mapper-spring-boot-starter

${mapper.starter.version}

com.github.pagehelper

pagehelper-spring-boot-starter

${pageHelper.starter.version}

mysql

mysql-connector-java

${mysql.version}

org.projectlombok

lombok

1.18.4

org.springframework.boot

spring-boot-maven-plugin

xmlns:xsi="http://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">

4.0.0

com.bes

spring-colud

1.0.0-SNAPSHOT

user-service

pom

org.springframework.boot

spring-boot-starter-parent

2.0.4.RELEASE

UTF-8

UTF-8

1.8

Finchley.SR1

2.0.3

5.1.32

1.2.5

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

tk.mybatis

mapper-spring-boot-starter

${mapper.starter.version}

com.github.pagehelper

pagehelper-spring-boot-starter

${pageHelper.starter.version}

mysql

mysql-connector-java

${mysql.version}

org.projectlombok

lombok

1.18.4

org.springframework.boot

spring-boot-maven-plugin

我的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 {

}

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.anAeYbAuDCnotation.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.annotaAeYbAuDCtion.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注解,如下图:AeYbAuDC

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

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

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

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

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

2、ALT+ENTER

3、选择第一个选项

4、在选择disable开头的选项

问题解决。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Date jpa 获取最新一条数据的实例代码
下一篇:解决Springboot @WebFilter拦截器未生效问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~