Skip to content

Commit 86d1a88

Browse files
authored
RestWithException and Unit Test
1 parent 10e80a0 commit 86d1a88

File tree

15 files changed

+390
-0
lines changed

15 files changed

+390
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.cg;
2+
3+
import org.springframework.boot.builder.SpringApplicationBuilder;
4+
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
5+
6+
public class ServletInitializer extends SpringBootServletInitializer {
7+
8+
@Override
9+
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
10+
return application.sources(StudentWebProjectApplication.class);
11+
}
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.cg;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class StudentWebProjectApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(StudentWebProjectApplication.class, args);
11+
}
12+
13+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.cg.controller;
2+
3+
import java.util.List;
4+
5+
import org.cg.exception.StudentNotFoundException;
6+
import org.cg.model.Student;
7+
import org.cg.service.StudentService;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.web.bind.annotation.ExceptionHandler;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.PathVariable;
13+
import org.springframework.web.bind.annotation.PostMapping;
14+
import org.springframework.web.bind.annotation.RequestBody;
15+
import org.springframework.web.bind.annotation.RequestMapping;
16+
import org.springframework.web.bind.annotation.RestController;
17+
18+
@RestController
19+
@RequestMapping("student")
20+
public class StudentController {
21+
@Autowired
22+
StudentService sService;
23+
@GetMapping
24+
public List<Student> printAllStudent() {
25+
return sService.getAllStudent();
26+
}
27+
@PostMapping
28+
public List<Student> saveStudent(@RequestBody Student obj) {
29+
return sService.createStudent(obj);
30+
}
31+
@GetMapping("/name/{name}")
32+
public List<Student> printAllStudentByName(@PathVariable String name) {
33+
return sService.getStudentByName(name);
34+
}
35+
@GetMapping("/{sid}")
36+
public Student findStudent(@PathVariable int sid) throws StudentNotFoundException{
37+
Student s=null;
38+
try {
39+
s=sService.getStudentByID(sid);
40+
}
41+
catch(Exception e) {
42+
throw new StudentNotFoundException("Student with given id is not available!");
43+
}
44+
return s;
45+
}
46+
}
47+
48+
49+
50+
51+
52+
53+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.cg.dao;
2+
3+
import java.util.List;
4+
5+
import org.cg.model.Student;
6+
7+
public interface StudentDao {
8+
/*public List<Student> createStudent(Student stu);
9+
public List<Student> getAllStudent();*/
10+
public List<Student> getStudentByName(String name);
11+
12+
13+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.cg.dao;
2+
3+
import java.util.List;
4+
5+
import javax.persistence.EntityManager;
6+
import javax.persistence.PersistenceContext;
7+
import javax.persistence.TypedQuery;
8+
9+
import org.cg.model.Student;
10+
import org.springframework.stereotype.Repository;
11+
import org.springframework.transaction.annotation.Transactional;
12+
13+
@Repository
14+
@Transactional
15+
public class StudentDaoImpl implements StudentDao{
16+
17+
@PersistenceContext
18+
EntityManager em;
19+
20+
/*@Override
21+
public List<Student> createStudent(Student stu) {
22+
em.persist(stu);
23+
return getAllStudent();
24+
}
25+
26+
@Override
27+
public List<Student> getAllStudent() {
28+
TypedQuery<Student> q=em.createQuery("select s from Student s",Student.class);
29+
return q.getResultList();
30+
}*/
31+
32+
@Override
33+
public List<Student> getStudentByName(String name) {
34+
35+
TypedQuery<Student> q=em.createQuery("select s from Student s where s.name like :n",Student.class);
36+
q.setParameter("n", name+"%");
37+
return q.getResultList();
38+
}
39+
40+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.cg.dao;
2+
3+
import org.cg.model.Student;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface StudentRepository extends StudentDao,JpaRepository<Student, Integer>{
9+
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.cg.exception;
2+
3+
import java.util.Date;
4+
5+
import javax.servlet.http.HttpServletRequest;
6+
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.web.bind.annotation.ControllerAdvice;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
import org.springframework.web.bind.annotation.ResponseBody;
11+
import org.springframework.web.bind.annotation.ResponseStatus;
12+
13+
@ControllerAdvice
14+
public class DemoException {
15+
@ResponseBody
16+
@ResponseStatus(value=HttpStatus.NOT_FOUND)
17+
@ExceptionHandler({Exception.class})
18+
public ErrorMapper handleConflict(Exception ex, HttpServletRequest req) {
19+
String msg=ex.getMessage();
20+
String uri=req.getRequestURL().toString();
21+
return new ErrorMapper(uri, msg, new Date());
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.cg.exception;
2+
3+
import java.util.Date;
4+
5+
public class ErrorMapper {
6+
private String url;
7+
private String message;
8+
private Date now;
9+
10+
public ErrorMapper(String url, String message, Date now) {
11+
super();
12+
this.url = url;
13+
this.message = message;
14+
this.now=now;
15+
}
16+
public String getUrl() {
17+
return url;
18+
}
19+
public void setUrl(String url) {
20+
this.url = url;
21+
}
22+
public String getMessage() {
23+
return message;
24+
}
25+
public void setMessage(String message) {
26+
this.message = message;
27+
}
28+
public Date getNow() {
29+
return now;
30+
}
31+
public void setNow(Date now) {
32+
this.now = now;
33+
}
34+
35+
36+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.cg.exception;
2+
3+
public class StudentNotFoundException extends Exception{
4+
public StudentNotFoundException() {
5+
6+
}
7+
public StudentNotFoundException(String message) {
8+
super(message);
9+
}
10+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.cg.model;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
@Entity
7+
public class Address {
8+
@Id
9+
private int aid;
10+
private String city;
11+
private String country;
12+
13+
public Address() {
14+
15+
}
16+
public Address(int aid, String city, String country) {
17+
super();
18+
this.aid = aid;
19+
this.city = city;
20+
this.country = country;
21+
}
22+
public int getAid() {
23+
return aid;
24+
}
25+
public void setAid(int aid) {
26+
this.aid = aid;
27+
}
28+
public String getCity() {
29+
return city;
30+
}
31+
public void setCity(String city) {
32+
this.city = city;
33+
}
34+
public String getCountry() {
35+
return country;
36+
}
37+
public void setCountry(String country) {
38+
this.country = country;
39+
}
40+
41+
42+
}

0 commit comments

Comments
 (0)