遇到的問題如下:
仁壽ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!1.Spring Boot正常啟動(dòng)后,訪問Controller報(bào)404
問題描述:
spring boot正常啟動(dòng),通過 http://localhost:8000/hello/first 訪問,一直報(bào)404
原因:
在搭建完項(xiàng)目之后,Application類是放在com.example.hello的包下面,而Controller類是放置在com.example.controller的包下面,導(dǎo)致spring boot無法掃描controller包下的內(nèi)容(默認(rèn)掃Application類對應(yīng)的包下的內(nèi)容)
解決措施:方法1:將controller包下的類移動(dòng)到hello包下
方法2:在啟動(dòng)上方添加@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注入時(shí),工程一直無法啟動(dòng),并報(bào)
Parameter 0 of constructor in com.example.controller.ReadingListController required a bean of type 'com.example.model.ReadingListRepository' that could not be found.
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的初始框架中,啟動(dòng)類位置于所有Service,Entity,Controller或者其它類的最上層的話,這個(gè)問題很少會(huì)出現(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小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文題目:記一次Springboot搭建過程-創(chuàng)新互聯(lián)
瀏覽路徑:http://aaarwkj.com/article34/ccjhpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、電子商務(wù)、網(wǎng)站建設(shè)、外貿(mào)建站、營銷型網(wǎng)站建設(shè)、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容