java - 出现白页错误,无法在 REST API 中找到我的问题?

标签 java rest api spring-boot

我正在构建一个 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);
    }

}

我的 Controller :

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

}

我的仓库:

package com.skilldistillery.myRest.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.skilldistillery.edgemarketing.entities.House;

@Repository
public interface HouseRepo extends JpaRepository<House, Integer>  {


}

我的服务:

package com.skilldistillery.myRest.services;

import java.util.List;

import org.springframework.stereotype.Service;

import com.skilldistillery.edgemarketing.entities.House;

@Service
public interface HouseService {

    List<House> index(); 

    House show(Integer id); 
}

还有我的 ServiceImpl:

package com.skilldistillery.myRest.services;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.skilldistillery.edgemarketing.entities.House;
import com.skilldistillery.myRest.repositories.HouseRepo;

@Service
public class HouseServiceImpl {
    @Autowired
    HouseRepo hRepo; 

    public House show(Integer id) {
        Optional<House> opt = hRepo.findById(id); 
        House house = null;
        if (opt.isPresent()) {
            house = opt.get();
        }
        return house;
    }

}

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

最佳答案

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

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

然后您的 Controller 将更改为以下内容。利用依赖注入(inject)

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

HouseServiceImpl 还应该实现 HouseService

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

}

*NB - 不要忘记删除以下配置@Autowired、@Repository,因为它们现在是在 BusinessConfig 类中处理的.更多的Bean可以在BusinessConfig类中定义

关于java - 出现白页错误,无法在 REST API 中找到我的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55460147/

相关文章:

java - 如何扩展 JavaFX 分页导航以显示附加控件?

java - 使用 REST API 和 HttpClient 时在 Java 中打印 JSON 响应

java - 如何使用 Java 导出 Windows 注册表文件

java - 从数据库 SQlite 中的 jTable 存储值进行不区分大小写的搜索(IDE : netbeans)

java - 是否可以使用 Spring 或 boot 来验证 URI 路径参数是否为空?

java - 其余输出与控制台与 Tomcat localhost 不同

ios - CVPixelBufferGetBaseAddress 返回空 VTDecompressionSessionDecodeFrame 回调

node.js - 如何让 Google API 返回 node.js 上两点之间的距离并将其存储在变量中?

java - GAE 端点错误 : can not access a member of class with modifiers "private"

java - 如何在 ARCore 中叠加二维图像?