Difference between revisions of "SpringBoot"

Line 4: Line 4:
 
==SpringBoot - Maven - Pom.xml==
 
==SpringBoot - Maven - Pom.xml==
  
<source>
+
<syntaxhighlight lang="xml">
  
 
<project xmlns="http://maven.apache.org/POM/4.0.0"
 
<project xmlns="http://maven.apache.org/POM/4.0.0"
Line 34: Line 34:
 
</project>
 
</project>
  
</source>
+
</syntaxhighlight>
  
 
to get latest Spring Boot Starter Parent pom.xml configurations visit : [https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent]
 
to get latest Spring Boot Starter Parent pom.xml configurations visit : [https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent]
Line 61: Line 61:
 
==SpringBoot - Controller Class - GET, POST, PUT, DELETE==
 
==SpringBoot - Controller Class - GET, POST, PUT, DELETE==
  
<source>
+
<syntaxhighlight lang="java">
 
package com.oasissofttech.springboot.brand;
 
package com.oasissofttech.springboot.brand;
  
Line 106: Line 106:
 
}
 
}
  
</source>
+
</syntaxhighlight>
  
  
 
==SpringBoot - Service Class==
 
==SpringBoot - Service Class==
  
<source>
+
<syntaxhighlight lang="java">
 
package com.oasissofttech.springboot.brand;
 
package com.oasissofttech.springboot.brand;
  
Line 163: Line 163:
 
}
 
}
  
</source>
+
</syntaxhighlight>
  
 
==Others==
 
==Others==
  
<source>
+
<syntaxhighlight lang="java">
 
package com.oasissofttech.springboot.brand;
 
package com.oasissofttech.springboot.brand;
  
Line 212: Line 212:
 
}
 
}
  
</source>
+
</syntaxhighlight>

Revision as of 11:09, 3 June 2018


SpringBoot - Maven - Pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.oasissoftech.springboot</groupId>
	<artifactId>SpringBoot-Tutorial-1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBoot-Tutorial-1</name>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<properties>
		<java.version>1.8</java.version>
	</properties>

</project>

to get latest Spring Boot Starter Parent pom.xml configurations visit : https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent

to get latest Spring Boot Web Starter pom.xml configurations visit : https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web


SpringBoot - Application Boot

Spring Boot application startup class :

package com.oasissofttech.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootTutorial1App {
	public static void main(String[] args) {
		SpringApplication.run(SpringBootTutorial1App.class,args);
	}
}


SpringBoot - Controller Class - GET, POST, PUT, DELETE

package com.oasissofttech.springboot.brand;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BrandController {
	@Autowired
	private BrandService brandService;
	
	@RequestMapping("/brands")
	public List<Brand> getAllBrands() {
		return brandService.getAllBrands();
	}
	
	@RequestMapping("/brand/{brandId}")
	public Brand getBrand(@PathVariable("brandId") int id) {
		return brandService.getBrand(id);
	
	}
	
	@RequestMapping(method=RequestMethod.POST,value="/brands")
	public void addBrand(@RequestBody Brand brand) {
		brandService.addBrand(brand);
	}
	
	@RequestMapping(method=RequestMethod.PUT,value="/brands/{id}")
	public void addBrand(@RequestBody Brand brand,@PathVariable int id) {
		brandService.updateBrand(brand,id);
	}
	
	@RequestMapping(method=RequestMethod.DELETE,value="/brands/{id}")
	public void deleteBrand(@PathVariable int id) {
		brandService.deleteBrand(id);
	}
	
}


SpringBoot - Service Class

package com.oasissofttech.springboot.brand;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Service;

@Service
public class BrandService {

	private List<Brand> brands = new ArrayList<>(Arrays.asList(
			new Brand(1,"Aston Martin","(1913–present)"),
			new Brand(2,"Bentley","(1919–present)"),
			new Brand(3,"Jaguar Land Rover","(2013-present)"),
			new Brand(4,"Lotus","(1952–present)"),
			new Brand(5,"Rolls Royce","(1904–present)")
			));
	
	public List<Brand> getAllBrands(){
		return brands;
	}
	
	
	public Brand getBrand(int id) {
		return brands.stream().filter(b->b.getId()==id).findFirst().get();
	}


	public void addBrand(Brand brand) {
		brands.add(brand);
	}


	public void updateBrand(Brand brand, int id) {
		int c = brands.size();
		for(int i=0;i<c;i++) {
			if(brands.get(i).getId()==id) {
				brands.set(i, brand);
				return;
			}
		}
	}


	public void deleteBrand(int id) {
		brands.removeIf(t->t.getId()==id);
	}
	
}

Others

package com.oasissofttech.springboot.brand;

public class Brand {
	private int id;
	private String brandName;
	private String description;

	public Brand() {
	}

	public Brand(int id, String brandName, String description) {
		super();
		this.id = id;
		this.brandName = brandName;
		this.description = description;
	}


	public int getId() {
		return id;
	}

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

	public String getBrandName() {
		return brandName;
	}

	public void setBrandName(String brandName) {
		this.brandName = brandName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}