获取白页错误,在 REST API 中找不到我的问题?

我正在构建一个REST API来访问数据库,并且遇到麻烦/持续出现白页错误。在圆圈中运行,试图在程序的流程或逻辑中找到我的错误和/或我的错误。


这是我的应用程序:


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); 

    }


}


它编译并启动,但通过邮递员和浏览器,我得到白页错误。我在互联网上搜索过,试图了解我哪里出了问题,但没有找到它。请指教。


紫衣仙女
浏览 114回答 1
1回答

天涯尽头无女友

您可以使用以下解决方案。将主类更改为以下代码@SpringBootApplicationpublic class MyrestapplicationApplication&nbsp; {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; SpringApplication.run(MyrestapplicationApplication.class, args);&nbsp; &nbsp; }}然后为您的配置创建一个单独的类。以及摆脱紧密耦合的架构。@Configuration@EntityScan("com.skilldistillery.edgemarketing.entities")@EnableJpaRepositories("com.skilldistillery.myRest.repositories")public class BusinessConfig {&nbsp; &nbsp; @Bean&nbsp; &nbsp; public HouseService houseService(final HouseRepo houseRepo){&nbsp; &nbsp; &nbsp; &nbsp; return new HouseServiceImpl(houseRepo);&nbsp; &nbsp; }&nbsp; &nbsp;}然后,您的控制器将更改为以下内容。利用依赖注入@RestController@RequestMapping("api")&nbsp;@CrossOrigin({ "*", "http://localhost:4200" })public class HouseController {&nbsp; &nbsp;private&nbsp; &nbsp;HouseService houseServ;&nbsp; &nbsp; public HouseController(HouseService houseServ) {&nbsp; &nbsp; &nbsp; &nbsp; this.houseServ = houseServ;&nbsp; &nbsp; }&nbsp; &nbsp; @GetMapping(value = "index/{id}",produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)&nbsp; &nbsp; public House show(@PathVariable("id") Integer id) {&nbsp; &nbsp; &nbsp; &nbsp; return houseServ.show(id);&nbsp;&nbsp; &nbsp; }}家庭服务简单也应该实施家庭服务public class HouseServiceImpl implements HouseService{&nbsp; private&nbsp; HouseRepo hRepo;&nbsp; &nbsp; public HouseServiceImpl(HouseRepo hRepo) {&nbsp; &nbsp; &nbsp; &nbsp; this.hRepo = hRepo;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public List<House> index() {&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; public House show(Integer id) {&nbsp; &nbsp; &nbsp; &nbsp; Optional<House> opt = hRepo.findById(id);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; House house = new House();&nbsp; &nbsp; &nbsp; &nbsp; if (opt.isPresent()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; house = opt.get();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return house;&nbsp; &nbsp; }}*注意 - 不要忘记删除以下配置,因为它们现在在类中处理。可以在类中定义更多 Bean@Autowired,@RepositoryBusinessConfigBusinessConfig
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java