为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

紫衣仙女 2022-09-14 16:07:16
我正在构建一个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);     }}它编译并启动,但通过邮递员和浏览器,我得到白页错误。我在互联网上搜索过,试图了解我哪里出了问题,但没有找到它。请指教。
查看完整描述

1 回答

?
天涯尽头无女友

TA贡献1831条经验 获得超9个赞

您可以使用以下解决方案。将主类更改为以下代码


@SpringBootApplication

public class MyrestapplicationApplication  {

    public static void main(String[] args) {

        SpringApplication.run(MyrestapplicationApplication.class, args);

    }


}

然后为您的配置创建一个单独的类。以及摆脱紧密耦合的架构。


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

    }   

}

然后,您的控制器将更改为以下内容。利用依赖注入


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

    }

}

家庭服务简单也应该实施家庭服务


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;

    }


}

*注意 - 不要忘记删除以下配置,因为它们现在在类中处理。可以在类中定义更多 Bean@Autowired,@RepositoryBusinessConfigBusinessConfig


查看完整回答
反对 回复 2022-09-14
  • 1 回答
  • 0 关注
  • 113 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信