spring-boot遇到问题总结

本文记录了Spring Boot新手在项目实践中遇到的问题及解决方案,包括环境配置导致的404错误、spring-data-jpa访问数据库失败等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近项目中,需要提供接口服务,主要是使用Spark跑批量任务,web服务只提供简单接口来设置参数,以及启动、停止spark任务等,所以考虑使用spring-boot,也当做业余的一个学习。

作为一个spring-boot新手,网上找个例子来开始学习,遇到问题会慢慢的追加到本文中。

1. Spring-boot环境配置后,运行提示404

项目代码结构
访问页面404

解决:404代表找不到访问地址,考虑是不是该url不存在或者敲错了,查证后没问题,那么下一个问题就是是不是spring并没有把相应的bean注册进去?
查看下,发现没有配置扫描的包,添加扫描包配置:
在入口类的@SpringBootApplication中添加scanBasePackages 配置具体的包:@SpringBootApplication(scanBasePackages = “com.pnlorf”)
重新启动,访问成功。
修改后成功访问

2. 使用spring-data-jpa访问数据库,启动报错

异常信息如下:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-05-15 09:59:37.292 ERROR 7016 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userRepository in com.pnlorf.controller.UserController required a bean of type 'com.pnlorf.dao.UserRepository' that could not be found.


Action:

Consider defining a bean of type 'com.pnlorf.dao.UserRepository' in your configuration.

源代码如下:
Controller:

package com.pnlorf.controller;

import com.pnlorf.dao.UserRepository;
import com.pnlorf.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * description:
 * author: pnlorf
 * date: 2018/5/14
 * package: com.pnlorf.controller
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @RequestMapping("queryAll")
    @ResponseBody
    public List<User> queryAll() {
        List<User> list = userRepository.findAll();
        return list;
    }
}

UserRepository:

package com.pnlorf.dao;

import com.pnlorf.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * description:
 * author: pnlorf
 * date: 2018/5/14
 * package: com.pnlorf.dao
 */
public interface UserRepository extends JpaRepository<User, String> {
}

User:

package com.pnlorf.model;

import javax.persistence.*;
import java.io.Serializable;

/**
 * description:
 * author: pnlorf
 * date: 2018/5/14
 * package: com.pnlorf.model
 */
@Entity
@Table(name = "user")
public class User implements Serializable {

    @Id
    @Column(name = "id")
    private String id;

    @Column(name = "name")
    private String name;

    @Column(name = "password")
    private String password;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

解决:google了一下,是说这种情况下,在Application入口类包下的都会被扫描,不然需要配置scanpackage,但是配置了扫描包没有生效(可能配置有问题吧),将SpringBootSparkGraphxApplication拿倒外层包,问题解决。
代码结构
访问成功
参考:
https://stackoverflow.com/questions/42907553/field-required-a-bean-of-type-that-could-not-be-found-error-spring-restful-ap?noredirect=1&lq=1

3.
[INFO] +- org.springframework.boot:spring-boot-starter-websocket:jar:2.2.5.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.2.5.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.2.5.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.2.5.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-validation:jar:2.2.5.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter:jar:2.2.5.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot:jar:2.2.5.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.5.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.5.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-devtools:jar:2.2.5.RELEASE:compile (optional) [INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:2.2.5.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-aop:jar:2.2.5.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.2.5.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter-data-redis:jar:2.2.5.RELEASE:compile [INFO] +- com.alibaba:druid-spring-boot-starter:jar:1.2.8:compile [INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.2.5.RELEASE:test [INFO] | +- org.springframework.boot:spring-boot-test:jar:2.2.5.RELEASE:test [INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.2.5.RELEASE:test
最新发布
03-17
<think>好的,用户现在发了一个关于Spring Boot依赖树的日志,询问如何查看版本。之前我已经回答过五种方法,用户可能现在遇到了具体的依赖树,需要进一步确认版本信息。 首先,用户提供的日志中有多行依赖项,每个都带有版本号2.2.5.RELEASE。我需要指出这些信息都一致,说明整个项目使用的是Spring Boot 2.2.5版本。需要确认用户是否理解如何从这些行中提取版本号,或者是否存在疑问。 其次,用户可能想知道为什么所有模块的版本都相同,是否需要检查是否有不一致的地方。这里需要解释Maven或Gradle的依赖管理机制,父POM如何统一版本,确保各模块版本一致,避免冲突。 另外,用户可能对2.2.5.RELEASE这个版本的状态有疑问,比如是否过时,是否需要升级。这时候可能需要提供版本的生命周期信息,比如该版本是否还在维护,是否有安全漏洞需要处理。 还要注意用户之前提到过JDK版本的问题,可能在使用过程中遇到兼容性问题,比如Spring Boot 2.2.5对应的JDK版本是什么,是否支持用户当前的环境。需要结合之前的交流,给出兼容性建议。 另外,用户提供的依赖树中有一些starter,比如websocket、web、data-jpa、data-redis等,可能需要提醒用户检查这些依赖是否都是项目需要的,避免引入不必要的库,增加项目体积。 最后,用户可能想确认是否有更好的方法来验证版本,比如使用Maven命令或者代码中获取版本号,是否需要进一步的步骤来确保版本正确无误。需要总结关键点,确保用户能够明确版本信息,并了解后续行动的建议。</think>--- ### **分析结果与关键信息提取** #### **1. Spring Boot 版本确认** 从您提供的依赖树中,所有Spring Boot组件版本均显示为 `2.2.5.RELEASE`: ```text [INFO] +- org.springframework.boot:spring-boot-starter-websocket:jar:2.2.5.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.2.5.RELEASE:compile ...(其他依赖项均使用相同版本) ``` **结论**:当前项目使用的 **Spring Boot 版本为 `2.2.5.RELEASE`**。 --- #### **2. 版本统一性验证** 所有Spring Boot相关依赖(如`spring-boot-starter-web`、`spring-boot-starter-data-jpa`等)均保持版本一致(`2.2.5.RELEASE`),说明项目中已正确通过 **父POM继承** 或 **依赖管理模块** 统一管理版本,避免了依赖冲突。 --- #### **3. 版本生命周期与兼容性参考** | Spring Boot 版本 | 发布日期 | 官方维护状态 | 兼容JDK范围 | |-------------------|-------------|---------------------|------------------| | 2.2.5.RELEASE | 2020-02-20 | **已结束支持**(EOL)| JDK 8-13 | **关键建议**: - ❗️ 此版本已超过官方维护周期(Spring Boot 2.2.x 的维护已于 2020-08-20 结束),**存在安全风险**。 - 推荐升级至 **Spring Boot 2.7.x(L
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值