Skip to content

Commit 9ba0497

Browse files
committed
Initial commit
0 parents  commit 9ba0497

15 files changed

+411
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.settings/
2+
3+
target/
4+
.classpath

.project

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>spring-boot-rest-design</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.springframework.ide.eclipse.core.springbuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.m2e.core.maven2Builder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
</buildSpec>
24+
<natures>
25+
<nature>org.springframework.ide.eclipse.core.springnature</nature>
26+
<nature>org.eclipse.jdt.core.javanature</nature>
27+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
28+
</natures>
29+
</projectDescription>

pom.xml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.nishanthd</groupId>
6+
<artifactId>spring-boot-rest-design</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>spring-boot-rest-design</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<parent>
14+
<groupId>org.springframework.boot</groupId>
15+
<artifactId>spring-boot-starter-parent</artifactId>
16+
<version>2.0.1.RELEASE</version>
17+
</parent>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<java.version>1.8</java.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-web</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
<version>3.8.1</version>
33+
<scope>test</scope>
34+
</dependency>
35+
</dependencies>
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-maven-plugin</artifactId>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
45+
</project>
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ndevs.fridge;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
*
8+
* @author Nishanth Dharmaraju
9+
*
10+
*/
11+
@SpringBootApplication
12+
public class App {
13+
public static void main(String[] args) {
14+
SpringApplication.run(App.class, args);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.ndevs.fridge;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.web.bind.annotation.ControllerAdvice;
5+
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
7+
8+
import com.ndevs.fridge.dto.ErrorResponse;
9+
import com.ndevs.fridge.exception.NoItemFoundException;
10+
11+
/**
12+
*
13+
* @author Nishanth Dharmaraju
14+
*
15+
*/
16+
@ControllerAdvice
17+
public class FridgeControllerAdvice {
18+
19+
/**
20+
* Called when no handler defined for a corresponding exception
21+
*
22+
* @param exception
23+
* @return
24+
*/
25+
@ExceptionHandler(Exception.class)
26+
public ResponseEntity<ErrorResponse> globalException(Exception exception) {
27+
return ResponseEntity.unprocessableEntity().body(new ErrorResponse("serverError", "Internal server error"));
28+
}
29+
30+
@ExceptionHandler(NoItemFoundException.class)
31+
public ResponseEntity<ErrorResponse> noItemFound(NoItemFoundException exception) {
32+
33+
return ResponseEntity.ok(new ErrorResponse(exception.getErrorCode(), exception.getMessage()));
34+
35+
}
36+
37+
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
38+
public ResponseEntity<ErrorResponse> mismatch(MethodArgumentTypeMismatchException exception) {
39+
40+
return ResponseEntity.badRequest()
41+
.body(new ErrorResponse(exception.getErrorCode(), exception.getLocalizedMessage()));
42+
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.ndevs.fridge.controller;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import com.ndevs.fridge.dto.FridgeItem;
12+
import com.ndevs.fridge.exception.NoItemFoundException;
13+
import com.ndevs.fridge.service.FridgeService;
14+
15+
/**
16+
*
17+
* @author Nishanth Dharmaraju
18+
*
19+
*/
20+
@RestController
21+
@RequestMapping("/fridge")
22+
public class FridgeController {
23+
24+
@Autowired
25+
FridgeService fridgeService;
26+
27+
@RequestMapping("/")
28+
public ResponseEntity<List<FridgeItem>> getAllItems() {
29+
30+
return ResponseEntity.ok(fridgeService.getAllItems());
31+
}
32+
33+
@RequestMapping("/{id}")
34+
public ResponseEntity<FridgeItem> getItemById(@PathVariable("id") Integer itemId) throws NoItemFoundException {
35+
36+
return ResponseEntity.ok(fridgeService.getItemByType(itemId));
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.ndevs.fridge.dto;
2+
3+
public class ErrorResponse {
4+
5+
private String error;
6+
private String description;
7+
8+
public ErrorResponse(String error, String description) {
9+
super();
10+
this.error = error;
11+
this.description = description;
12+
}
13+
14+
public String getError() {
15+
return error;
16+
}
17+
18+
public void setError(String error) {
19+
this.error = error;
20+
}
21+
22+
public String getDescription() {
23+
return description;
24+
}
25+
26+
public void setDescription(String description) {
27+
this.description = description;
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.ndevs.fridge.dto;
2+
3+
public class FridgeItem {
4+
5+
private Integer itemType;
6+
7+
private String name;
8+
9+
private Integer quantity;
10+
11+
public Integer getItemType() {
12+
return itemType;
13+
}
14+
15+
public void setItemType(Integer itemType) {
16+
this.itemType = itemType;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public Integer getQuantity() {
28+
return quantity;
29+
}
30+
31+
public void setQuantity(Integer quantity) {
32+
this.quantity = quantity;
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.ndevs.fridge.exception;
2+
3+
/**
4+
* Abstract superclass for all exceptions thrown in the controller package and
5+
* subpackages.
6+
*
7+
* This class is to force every exception class to define
8+
* {@code:getErrorCode()} method.
9+
*
10+
* @author Nishanth Dharmaraju
11+
*
12+
*/
13+
public abstract class NestedException extends Exception {
14+
15+
NestedException(String message) {
16+
super(message);
17+
}
18+
19+
/**
20+
* Returns error code of the exception
21+
*/
22+
public abstract String getErrorCode();
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.ndevs.fridge.exception;
2+
3+
/**
4+
* Exception thrown when finding zero item.
5+
*
6+
* @author Nishanth Dharmaraju
7+
*
8+
*/
9+
public class NoItemFoundException extends NestedException {
10+
11+
public static final String ERROR_CODE = "noItemFound";
12+
13+
public NoItemFoundException(String message) {
14+
super(message);
15+
}
16+
17+
@Override
18+
public String getErrorCode() {
19+
return ERROR_CODE;
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.ndevs.fridge.model;
2+
3+
public class FridgeItem {
4+
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.ndevs.fridge.service;
2+
3+
import java.util.List;
4+
5+
import com.ndevs.fridge.dto.FridgeItem;
6+
import com.ndevs.fridge.exception.NoItemFoundException;
7+
8+
/**
9+
*
10+
* @author Nishanth Dharmaraju
11+
*
12+
*/
13+
public interface FridgeService {
14+
15+
List<FridgeItem> getAllItems();
16+
17+
FridgeItem getItemByType(Integer itemType) throws NoItemFoundException;
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.ndevs.fridge.service.impl;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.springframework.stereotype.Service;
7+
8+
import com.ndevs.fridge.dto.FridgeItem;
9+
import com.ndevs.fridge.exception.NoItemFoundException;
10+
import com.ndevs.fridge.service.FridgeService;
11+
12+
/**
13+
*
14+
* @author Nishanth Dharmaraju
15+
*
16+
*/
17+
@Service
18+
public class FridgeServiceImpl implements FridgeService {
19+
20+
private static final int FRUIT = 1;
21+
private static final int DRINKS = 2;
22+
private static final int MEAT = 4;
23+
24+
@Override
25+
public List<FridgeItem> getAllItems() {
26+
27+
FridgeItem fruit = new FridgeItem();
28+
fruit.setItemType(FRUIT);
29+
fruit.setName("Fruit");
30+
fruit.setQuantity(10);
31+
FridgeItem drinks = new FridgeItem();
32+
drinks.setItemType(DRINKS);
33+
drinks.setName("Drinks");
34+
drinks.setQuantity(20);
35+
FridgeItem meat = new FridgeItem();
36+
meat.setItemType(MEAT);
37+
meat.setName("Meat");
38+
meat.setQuantity(3);
39+
40+
return Arrays.asList(fruit, drinks, meat);
41+
}
42+
43+
@Override
44+
public FridgeItem getItemByType(Integer itemType) throws NoItemFoundException {
45+
46+
FridgeItem item = null;
47+
48+
if (itemType.equals(FRUIT)) {
49+
item = new FridgeItem();
50+
item.setItemType(FRUIT);
51+
item.setName("Fruit");
52+
item.setQuantity(10);
53+
} else {
54+
throw new NoItemFoundException("No such item found");
55+
}
56+
57+
return item;
58+
}
59+
60+
}

src/main/resources/application.properties

Whitespace-only changes.

0 commit comments

Comments
 (0)