如何修复spring boot中的500个内部错误

nwnhqdif  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(390)

我在解决restapi中的500个内部错误时遇到了麻烦。
当我使get和post请求正常工作,但当我使put、delete和findbyid get请求不工作时。如果我尝试发出请求,它将显示500个内部错误。我不知道为什么会这样,所以请帮帮我。提前谢谢!家庭1.班级

@Entity
public class Family1 {

    @Id 
    private long id;
    private String Name;
    private int Member;
    private long contactNo;
    @Embedded
    public Address address;
// getters,setters

Family1控制器类

@RestController
@RequestMapping(value = "/family1")
public class Family1Controller {

    @Autowired
    private Family1Service family1Service;

@RequestMapping("/fam1")
    public List<Family1> getAllFamily1(){
        return family1Service.getAllFamily1();
    }
@RequestMapping(method=RequestMethod.POST,value="/fam1") 
    public void addFamily1(@RequestBody Family1 family1){
        family1Service.addFamily1(family1);   
     }
@RequestMapping(method=RequestMethod.GET,value="/fam1/{id}")
    public Family1 getFamily1Id(@PathVariable long id) {
        List<Family1> family=family1Service.getFamily1Id(id);
        return family.get((int) id);
}
@RequestMapping(method=RequestMethod.PUT,value="/fam1/{id}")
     public Family1 updateFamily1(@RequestBody Family1 family1, @PathVariable("id") long id){
        family1.setId(id);
        return family1Service.updateFamily1(family1);   
    }
@RequestMapping(method=RequestMethod.DELETE,value="/fam1/{id}")
    public void deleteFamily1(@PathVariable("id") long id) {
        family1Service.deleteFamily1(id);
    }

}

family1service.class系列

@Service
public class Family1Service {

    private Family1Repository family1Repository;

    private List<Family1> family=new ArrayList<>(Arrays.asList(
            new Family1(1,"abc",3,32423423,new Address("east street","xxxxxx",23456)),
            new Family1(2,"xyz",4,354646,new Address("west street","yyyyyy",23456)),
            ));

    public  List<Family1> getAllFamily1() {
        return family;
    }
    public List<Family1> getFamily1Id(long id){
        family1Repository.findById(id);
        return family;
    }
    public void addFamily1(Family1 family1) {
        family.add(family1);
    }

    public Family1 updateFamily1(Family1 family1) {
        return family1Repository.save(family1); 
    }
    public void deleteFamily1(long id) {
        family1Repository.deleteById(id);
    }
}

Family1存储库.class

public interface Family1Repository extends CrudRepository<Family1, String>  {

    public void deleteById(long id);

    public void save(Family1 family1, long id);

    public void findById(long id);

family1application.class类

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
public class Family1Application {

    public static void main(String[] args) {
        SpringApplication.run(Family1Application.class, args);

            }

}

错误:

{
    "timestamp": "2021-01-06T07:32:49.232+00:00",
    "status": 500,
    "error": "Internal Server Error",
    "message": "",
    "path": "/family1/fam1/1"
}
qfe3c7zg

qfe3c7zg1#

您将对预定义列表的调用与对存储库的调用混合在一起。列表中的元素在数据库中没有表示形式,因此使用这些ID删除存储库中的对象时,这些元素无效。
您可能需要尝试以下操作:

@Service
public class Family1Service {

    private Family1Repository family1Repository;

    public  List<Family1> getAllFamily1() {
        return family1Repository.findAll();
    }

    public Family1 getFamily1Id(long id){
        return family1Repository.findById(id).orElseThrow(); 
        // This returns an Optional orElseThrow will create an error if nothing is found
    }

    public void addFamily1(Family1 family1) {
        family1Repository.save(family1);
    }

    public Family1 updateFamily1(Family1 family1) {
        return family1Repository.save(family1); 
    }

    public void deleteFamily1(long id) {
        family1Repository.deleteById(id);
    }
}

在控制器中:

@RestController
@RequestMapping(value = "/family1")
public class Family1Controller {

    @Autowired
    private Family1Service family1Service;

    @RequestMapping("/fam1")
    public List<Family1> getAllFamily1(){
        return family1Service.getAllFamily1();
    }

    @RequestMapping(method=RequestMethod.POST, value="/fam1") 
    public void addFamily1(@RequestBody Family1 family1){
        family1Service.addFamily1(family1);   
     }

    @RequestMapping(method=RequestMethod.GET, value="/fam1/{id}")
    public Family1 getFamily1Id(@PathVariable long id) {
        return family1Service.getFamily1Id(id);
     }

     @RequestMapping(method=RequestMethod.PUT, value="/fam1")
     public Family1 updateFamily1(@RequestBody Family1 family1){
        return family1Service.updateFamily1(family1);   
    }

    @RequestMapping(method=RequestMethod.DELETE, value="/fam1/{id}")
    public void deleteFamily1(@PathVariable("id") long id) {
        family1Service.deleteFamily1(id);
    }
}
public interface Family1Repository extends CrudRepository<Family1, long>  {
    // All repository methods you need are already provided by the CrudRepository
}

我删除了你在 Family1Service 以避免数据库中不存在数据。这可能导致调用 findAll() 存储库的方法。
注意,我没有时间完全测试这个,但是一般的方法应该可以工作。

相关问题