Java 获取白页错误,在REST API中找不到我的问题?
我正在构建一个RESTAPI来访问一个数据库,但遇到了问题/始终出现了一个白页错误。在循环中运行,试图在程序流或逻辑中查找我的错误和/或我的错误 这是我的申请表:Java 获取白页错误,在REST API中找不到我的问题?,java,rest,api,spring-boot,Java,Rest,Api,Spring Boot,我正在构建一个RESTAPI来访问一个数据库,但遇到了问题/始终出现了一个白页错误。在循环中运行,试图在程序流或逻辑中查找我的错误和/或我的错误 这是我的申请表: package com.skilldistillery.myRest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.
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.skilldillery.myRest.repositories;
导入org.springframework.data.jpa.repository.JpaRepository;
导入org.springframework.stereotype.Repository;
import com.skillDillery.edgemarketing.entities.House;
@存储库
公共接口HouseRepo扩展了JpaRepository{
}
我的服务:
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);
}
package com.skilldillery.myRest.services;
导入java.util.List;
导入org.springframework.stereotype.Service;
import com.skillDillery.edgemarketing.entities.House;
@服务
公共接口家庭服务{
列表索引();
房展(整数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;
}
}
package com.skilldillery.myRest.services;
导入java.util.Optional;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Service;
import com.skillDillery.edgemarketing.entities.House;
导入com.skilldillery.myRest.repositories.HouseRepo;
@服务
公共类房屋服务impl{
@自动连线
赫雷波之家;
酒店展览(整数id){
可选opt=hRepo.findById(id);
House=null;
if(opt.isPresent()){
house=opt.get();
}
归还房屋;
}
}
它编译并启动,但通过邮递员和浏览器,我得到了白页错误。我在互联网上搜索,试图了解我的错误,但没有找到。请告知 您可以使用以下解决方案。 将主类更改为以下代码
@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;
}
}
公共类HouseServiceImpl实现HouseService{
赫雷波私人住宅;
公共住房服务项目(住房重建和住房政策办公室){
this.hRepo=hRepo;
}
@凌驾
公共列表索引(){
返回null;
}
酒店展览(整数id){
可选opt=hRepo.findById(id);
房子=新房子;
if(opt.isPresent()){
house=opt.get();
}
归还房屋;
}
}
*NB-不要忘记删除以下配置
@Autowired,@Repository
,因为它们现在在BusinessConfig
类中处理。可以在BusinessConfig
类中定义更多bean您可以共享来自postman/browser的HTTP请求吗?我还看到你添加了交叉原点注释,这是为了什么?谢谢@Adi Ohana。以下是邮递员给我的回复:{“timestamp”:“2019-04-01T17:22:39.178+0000”,“status”:404,“error”:“Not Found”,“message”:“No message available”,“path”:“/api/index/245034”}我相信(d)交叉源代码的原因是为了克服交叉源代码错误。以前教我的例子就是用它;我愿意接受批评/学习。CrossOrigin没有错。它只意味着你的资源可以被任何客户使用。谢谢!这就成功了。非常感谢你的帮助。我将分析这项工作的原因,并将其与我所做的工作进行比较。再次感谢你。