---
# System prepended metadata

title: SpringBoot Rest API Example
tags: [java]

---

# SpringBoot Rest API Example

###### tags: `java`

[TOC]

Eclipse -> File -> New -> SpringBoot -> Spring Starter Project

![](https://i.imgur.com/KOwVSxL.png)

![](https://i.imgur.com/qRPDzLE.png)


### pom.xml dependency sinature

* #### Developer Tools

    1. Spring Boot DevTools
    2. Lombok
    
* #### **SQL**

    1. Spring Data JPA
    2. Other brand SQL Driver
    
**pom.xml**
```xml=
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.0</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.atpumpkin</groupId>
	<artifactId>api</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>api</name>
	<description>Pumpkin Web Api</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

```

**application.yml**
```yaml=
server:
  port: 8091

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc.mysql://192.168.88.4/{DbName}
    username: root
    password: root
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQl5Dialect
```

**com.atpumpkin.api**


**com.atpumpkin.controller**
* ToolController.java

```java=
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.atpumpkin.entity.Tool;
import com.atpumpkin.service.ToolService;

@RestController
@RequestMapping("/api")
public class ToolController {

	@Autowired
	private ToolService service;

	@PostMapping("/tool")
	public ResponseEntity<Tool> addTool(@RequestBody Tool tool) {

		return new ResponseEntity<Tool>(service.saveTool(tool), HttpStatus.CREATED);
	}

	@PostMapping("/addTools")
	public List<Tool> addTools(@RequestBody List<Tool> tools) {

		return service.saveTools(tools);
	}

	@GetMapping("/getTool")
	public List<Tool> getAllTools() {

		return service.getAllTools();
	}

	@GetMapping("/toolById/{id}")
	public ResponseEntity<Tool> findToolById(@PathVariable("id") int id) {

		return new ResponseEntity<Tool>(service.getToolById(id), HttpStatus.OK);
	}

	@PutMapping("/update/{id}")
	public ResponseEntity<Tool> updateTool(@PathVariable("id") int id, @RequestBody Tool tool) {

		return new ResponseEntity<Tool>(service.updateTool(tool, id), HttpStatus.OK);
	}

	@DeleteMapping("/delete/{id}")
	public ResponseEntity<String> deleteTool(@PathVariable("id") int id) {

		service.deleteTool(id);

		return new ResponseEntity<String>("Deleted successfully ! ", HttpStatus.OK);
	}

}
```

**com.atpumpkin.entity**
* Tool.java

```java=
import java.sql.Date;
import java.sql.Timestamp;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "tool")
public class Tool {

	@Id
	private int id;
	private int tooltype1;
	private int tooltype2;
	private int tool_for_hero;
	private float effectrate;
	private float existrate;
	private Date create_at;
	private Timestamp modified_at;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getTooltype1() {
		return tooltype1;
	}

	public void setTooltype1(int tooltype1) {
		this.tooltype1 = tooltype1;
	}

	public int getTooltype2() {
		return tooltype2;
	}

	public void setTooltype2(int tooltype2) {
		this.tooltype2 = tooltype2;
	}

	public int getTool_for_hero() {
		return tool_for_hero;
	}

	public void setTool_for_hero(int tool_for_hero) {
		this.tool_for_hero = tool_for_hero;
	}

	public float getEffectrate() {
		return effectrate;
	}

	public void setEffectrate(float effectrate) {
		this.effectrate = effectrate;
	}

	public float getExistrate() {
		return existrate;
	}

	public void setExistrate(float existrate) {
		this.existrate = existrate;
	}

	public Date getCreate_at() {
		return create_at;
	}

	public void setCreate_at(Date create_at) {
		this.create_at = create_at;
	}

	public Timestamp getModified_at() {
		return modified_at;
	}

	public void setModified_at(Timestamp modified_at) {
		this.modified_at = modified_at;
	}

}

```

**com.atpumpkin.repository**
* ToolRepository.java

```java=
package com.atpumpkin.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.atpumpkin.entity.Tool;

@Repository
public interface ToolRepository extends JpaRepository<Tool, Integer>{
	

}
```

**com.atpumpkin.service**
* ToolService.java

```java=
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atpumpkin.entity.Tool;
import com.atpumpkin.exception.ResourceNotFoundException;
import com.atpumpkin.repository.ToolRepository;

@Service
public class ToolService {

	@Autowired
	private ToolRepository repository;

	public Tool saveTool(Tool tool) {

		return repository.save(tool);
	}

	public List<Tool> saveTools(List<Tool> tools) {

		return repository.saveAll(tools);
	}

	public List<Tool> getAllTools() {

		return repository.findAll();
	}

	public Tool getToolById(int id) {
		
//		Optional<Tool> tool = repository.findById(id);
//		
//		if(tool.isPresent()) {
//			
//			return tool.get();
//		}else {
//			
//			throw new ResourceNotFoundException("Tool", "Id", id);
//		}
		
		return repository.findById(id).orElseThrow( () -> 
				new ResourceNotFoundException("Tool", "Id", id));
	}
	
	public String deleteTool(int id) {
		
		repository.deleteById(id);
		
		return "Tool Removed !" + id;
	}
	
	public Tool updateTool(Tool tool, Integer id) {
		
		Tool tools = repository.findById(tool.getId()).orElseThrow( 
				() -> new ResourceNotFoundException("Tool", "Id", id));
		
		tools.setTooltype1(tool.getTooltype1());
		tools.setTooltype2(tool.getTooltype2());
		tools.setTool_for_hero(tool.getTool_for_hero());
		tools.setEffectrate(tool.getExistrate());
		tools.setExistrate(tool.getExistrate());
		tools.setCreate_at(tool.getCreate_at());
		
		return repository.save(tools);
	}
	
}
```