我正在构建一个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);
}
}
我的仓库:
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;
}
}
它可以编译并启动,但是通过邮递员和浏览器,我遇到了白页错误。我搜寻了互联网,试图了解我要去哪里,但没有找到它。请指教。
答案 0 :(得分:1)
您可以使用以下解决方案。 将您的主类更改为以下代码
@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);
}
}
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
类中处理的。可以在{{ 1}}类