Skip to content

Commit 04e54b2

Browse files
Added code for question 43888003.
1 parent f6aebc1 commit 04e54b2

14 files changed

+244
-0
lines changed

43888003/pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>org.example</groupId>
7+
<artifactId>stackoverflow</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>war</packaging>
10+
11+
<name>stackoverflow</name>
12+
<description></description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.3.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-aop</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-data-jpa</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-data-rest</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-web</artifactId>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>com.h2database</groupId>
47+
<artifactId>h2</artifactId>
48+
<scope>runtime</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-tomcat</artifactId>
53+
<scope>provided</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-starter-test</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
</dependencies>
61+
62+
<build>
63+
<plugins>
64+
<plugin>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-maven-plugin</artifactId>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
71+
72+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.example;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application
8+
{
9+
public static void main(String[] args)
10+
{
11+
SpringApplication.run(Application.class, args);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.example;
2+
3+
import org.springframework.boot.builder.SpringApplicationBuilder;
4+
import org.springframework.boot.web.support.SpringBootServletInitializer;
5+
6+
public class ServletInitializer extends SpringBootServletInitializer
7+
{
8+
@Override
9+
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
10+
{
11+
return application.sources(Application.class);
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.example.api;
2+
3+
public class ResourceNotFoundException extends RuntimeException
4+
{
5+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.example.api;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ControllerAdvice;
5+
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.bind.annotation.ResponseStatus;
7+
8+
@ControllerAdvice
9+
public class ResourceNotFoundExceptionHandler
10+
{
11+
@ExceptionHandler(ResourceNotFoundException.class)
12+
@ResponseStatus(HttpStatus.NOT_FOUND)
13+
public void handleResourceNotFound()
14+
{
15+
}
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.example.data;
2+
3+
import org.example.domain.Department;
4+
5+
public interface DepartmentRepository extends ModelRepository<Department>
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.example.data;
2+
3+
import org.example.domain.Employee;
4+
5+
public interface EmployeeRepository extends ModelRepository<Employee>
6+
{
7+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.example.data;
2+
3+
import org.aspectj.lang.annotation.AfterReturning;
4+
import org.aspectj.lang.annotation.Aspect;
5+
import org.example.api.ResourceNotFoundException;
6+
import org.springframework.stereotype.Component;
7+
8+
@Aspect
9+
@Component
10+
public class InvalidRepositoryReturnValueAspect
11+
{
12+
@AfterReturning(pointcut = "execution(* org.example.data.*Repository+.findOne(..))", returning = "result")
13+
public void intercept(final Object result)
14+
{
15+
if (result == null)
16+
{
17+
throw new ResourceNotFoundException();
18+
}
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.example.data;
2+
3+
import org.example.domain.Model;
4+
import org.springframework.data.repository.NoRepositoryBean;
5+
import org.springframework.data.repository.PagingAndSortingRepository;
6+
7+
@NoRepositoryBean
8+
public interface ModelRepository<T extends Model> extends PagingAndSortingRepository<T, Long>
9+
{
10+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.example.domain;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
6+
@Entity
7+
public class Department extends Model
8+
{
9+
@Column(length = 100, name = "name", nullable = false)
10+
private String name;
11+
12+
public String getName()
13+
{
14+
return name;
15+
}
16+
17+
public void setName(String name)
18+
{
19+
this.name = name;
20+
}
21+
}

0 commit comments

Comments
 (0)