Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

java - How to fix 500 internal error in spring boot

I'm Really getting trouble solving 500 internal error in restapi.

When I make GET and POST request working fine but when I make PUT,DELETE and findById GET request not working. if I try to make a request it will show 500 internal error. I don't know why it is happening so please help me. Thank you in Advance! Family1.class

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

Family1Controller.class

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

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

error:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are mixing the call to a predefined list with calls to the repository. The elements in the list have no representation in the database, therefore they are invalid when you use those IDs to delete objects with the repository.

You might want to try something like this:

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

And in the controller:

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

I removed the List you used in the Family1Service to avoid data not being present in the database. This might lead to an empty result when calling the findAll() method of the repository.

Note that I did not have time to fully test this, but the general approach should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...