记一次Springboot搭建过程-创新互联

遇到的问题如下:

仁寿ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

1.Spring Boot正常启动后,访问Controller报404
问题描述:

spring boot正常启动,通过 http://localhost:8000/hello/first 访问,一直报404

原因:

在搭建完项目之后,Application类是放在com.example.hello的包下面,而Controller类是放置在com.example.controller的包下面,导致spring boot无法扫描controller包下的内容(默认扫Application类对应的包下的内容)
解决措施:

方法1:将controller包下的类移动到hello包下

方法2:在启动上方添加@ComponentScan注解,此注解为指定扫描路径,例如:@ComponentScan(basePackages = {"com.example.controller"})

package com.example.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.example.controller"})
public class HelloApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }

}

2.无法注入继承JpaRepository的接口
问题描述:

如下代码该接口在继承JpaRepository后,在controller类中通过@Autowired注入时,工程一直无法启动,并报
Parameter 0 of constructor in com.example.controller.ReadingListController required a bean of type 'com.example.model.ReadingListRepository' that could not be found.

记一次Spring boot搭建过程

package com.example.model;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ReadingListRepository extends JpaRepository {

    List findByReader(String reader);
}
package com.example.controller;

import com.example.model.Book;
import com.example.model.ReadingListRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("/readingList")
public class ReadingListController {

    ReadingListRepository readingListRepository;

    @Autowired
    public ReadingListController(ReadingListRepository readingListRepository) {
        this.readingListRepository = readingListRepository;
    }

    @RequestMapping(value = "/{reader}", method = RequestMethod.GET)
    public String readersBooks(@PathVariable("reader") String reader, Model model) {
        List readingList = readingListRepository.findByReader(reader);
        if (readingList != null) {
            model.addAttribute("books", readingList);
        }
        return "readingList";
    }

    @RequestMapping(value = "/{reader}", method = RequestMethod.POST)
    public String addToReadingList(@PathVariable("reader") String reader, Book book) {
        book.setReader(reader);
        readingListRepository.save(book);
        return "redirect:/readingList/{reader}";
    }
}

原因:

Springboot未能正常将其扫描并没注入到容器中。而且一般在使用Springboot的初始框架中,启动类位置于所有Service,Entity,Controller或者其它类的最上层的话,这个问题很少会出现。

解决措施:

方案一、把 @SpringBootApplication 注解的 SpringBoot 入口类移到上层 root 包中,使 JpaRepository 子接口位于 root 包及其子包中。

方案二、在 SpringBoot 入口类上添加

(1) @ComponentScan(basePackages = "xxx.xxx.xxx"):扫描 @Controller、@Service 注解;
    (2) @EnableJpaRepositories(basePackages = "xxx.xxx.xxx"):扫描 @Repository 注解;
    (3) @EntityScan(basePackages = "xxx.xxx.xxx"):扫描 @Entity 注解;

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站标题:记一次Springboot搭建过程-创新互联
文章起源:http://myzitong.com/article/ccjhpe.html