成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

記一次Springboot搭建過程-創(chuàng)新互聯(lián)

遇到的問題如下:

仁壽ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

1.Spring Boot正常啟動后,訪問Controller報404
問題描述:

spring boot正常啟動,通過 http://localhost:8000/hello/first 訪問,一直報404

原因:

在搭建完項目之后,Application類是放在com.example.hello的包下面,而Controller類是放置在com.example.controller的包下面,導(dǎo)致spring boot無法掃描controller包下的內(nèi)容(默認掃Application類對應(yīng)的包下的內(nèi)容)
解決措施:

方法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<Book, Long> {

    List<Book> 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<Book> 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或者其它類的最上層的話,這個問題很少會出現(xiàn)。

解決措施:

方案一、把 @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 注解;

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

本文題目:記一次Springboot搭建過程-創(chuàng)新互聯(lián)
瀏覽路徑:http://jinyejixie.com/article34/ccjhpe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、電子商務(wù)網(wǎng)站建設(shè)、外貿(mào)建站營銷型網(wǎng)站建設(shè)、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)
兴城市| 宣恩县| 湖南省| 海兴县| 南京市| 时尚| 广宗县| 普兰店市| 县级市| 锦州市| 金华市| 克什克腾旗| 莱芜市| 凌源市| 石景山区| 三都| 汕尾市| 江山市| 梁平县| 公主岭市| 繁昌县| 福海县| 平邑县| 台江县| 保靖县| 巴塘县| 杭州市| 肥东县| 萨嘎县| 循化| 措美县| 芒康县| 彰化市| 迁安市| 大石桥市| 瑞安市| 林西县| 酉阳| 大关县| 恩平市| 永兴县|