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