第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

獲取白頁錯(cuò)誤,在 REST API 中找不到我的問題?

獲取白頁錯(cuò)誤,在 REST API 中找不到我的問題?

紫衣仙女 2022-09-14 16:07:16
我正在構(gòu)建一個(gè)REST API來訪問數(shù)據(jù)庫,并且遇到麻煩/持續(xù)出現(xiàn)白頁錯(cuò)誤。在圓圈中運(yùn)行,試圖在程序的流程或邏輯中找到我的錯(cuò)誤和/或我的錯(cuò)誤。這是我的應(yīng)用程序:package com.skilldistillery.myRest;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.domain.EntityScan;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;import org.springframework.context.annotation.ComponentScan;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;@SpringBootApplication@ComponentScan(basePackages= {"com.skilldistillery.edgemarketing"})@EntityScan("com.skilldistillery.edgemarketing")@EnableJpaRepositories("com.skilldistillery.myRest.repositories")public class MyRestApplication extends SpringBootServletInitializer {    @Override    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {        return application.sources(MyRestApplication.class);    }    public static void main(String[] args) {        SpringApplication.run(MyRestApplication.class, args);    }}我的控制器:package com.skilldistillery.myRest.controllers;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.CrossOrigin;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.skilldistillery.edgemarketing.entities.House;import com.skilldistillery.myRest.services.HouseService;@RestController@RequestMapping("api") @CrossOrigin({ "*", "http://localhost:4200" })public class HouseController {    @Autowired     HouseService houseServ;     @GetMapping("index/{id}")    public House show(@PathVariable("id") Integer id) {        return houseServ.show(id);     }}它編譯并啟動(dòng),但通過郵遞員和瀏覽器,我得到白頁錯(cuò)誤。我在互聯(lián)網(wǎng)上搜索過,試圖了解我哪里出了問題,但沒有找到它。請(qǐng)指教。
查看完整描述

1 回答

?
天涯盡頭無女友

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以使用以下解決方案。將主類更改為以下代碼


@SpringBootApplication

public class MyrestapplicationApplication  {

    public static void main(String[] args) {

        SpringApplication.run(MyrestapplicationApplication.class, args);

    }


}

然后為您的配置創(chuàng)建一個(gè)單獨(dú)的類。以及擺脫緊密耦合的架構(gòu)。


@Configuration

@EntityScan("com.skilldistillery.edgemarketing.entities")

@EnableJpaRepositories("com.skilldistillery.myRest.repositories")

public class BusinessConfig {


    @Bean

    public HouseService houseService(final HouseRepo houseRepo){

        return new HouseServiceImpl(houseRepo);

    }   

}

然后,您的控制器將更改為以下內(nèi)容。利用依賴注入


@RestController

@RequestMapping("api") 

@CrossOrigin({ "*", "http://localhost:4200" })

public class HouseController {


   private   HouseService houseServ;


    public HouseController(HouseService houseServ) {

        this.houseServ = houseServ;

    }


    @GetMapping(value = "index/{id}",produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)

    public House show(@PathVariable("id") Integer id) {

        return houseServ.show(id); 

    }

}

家庭服務(wù)簡(jiǎn)單也應(yīng)該實(shí)施家庭服務(wù)


public class HouseServiceImpl implements HouseService{

  private  HouseRepo hRepo;


    public HouseServiceImpl(HouseRepo hRepo) {

        this.hRepo = hRepo;

    }


    @Override

    public List<House> index() {

        return null;

    }


    public House show(Integer id) {

        Optional<House> opt = hRepo.findById(id); 

        House house = new House();

        if (opt.isPresent()) {

            house = opt.get();

        }

        return house;

    }


}

*注意 - 不要忘記刪除以下配置,因?yàn)樗鼈儸F(xiàn)在在類中處理。可以在類中定義更多 Bean@Autowired,@RepositoryBusinessConfigBusinessConfig


查看完整回答
反對(duì) 回復(fù) 2022-09-14
  • 1 回答
  • 0 關(guān)注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)