Simple Restful CURD operations(Student) Without DataBase


pom.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.wfd</groupId>
<artifactId>springDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springDemo</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
 <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.5.0</version>
 </dependency>
 <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.5.0</version>
 </dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>



app.java :-
------------
package com.wfd.springDemo;


import static com.google.common.collect.Lists.newArrayList;
import static springfox.documentation.schema.AlternateTypeRules.newRule;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.async.DeferredResult;

import com.fasterxml.classmate.TypeResolver;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.schema.WildcardType;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@SpringBootApplication
public class SpringDemoApplication {

public static void main(String[] args) {
System.out.println("main");
SpringApplication.run(SpringDemoApplication.class, args);
}

@Autowired
private TypeResolver typeResolver;

@Bean
public Docket pcfgradleApi() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build().pathMapping("/").genericModelSubstitutes(ResponseEntity.class)
.alternateTypeRules(newRule(
typeResolver.resolve(DeferredResult.class,
typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
typeResolver.resolve(WildcardType.class)))
.useDefaultResponseMessages(false)
.globalResponseMessage(RequestMethod.GET, newArrayList(new ResponseMessageBuilder().code(500)
.message("500 message").responseModel(new ModelRef("Error")).build()));
}
}


Model :-

public class Student {

public String name;
public int id;
}

Controller:-

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;

import com.wfd.demo.entity.Student;
import com.wfd.demo.service.StudentService;

/**
 * this class is used to take Student Rest calls.
 *
 * @author BayaReddy
 *
 */
@RequestMapping("/student")
@RestController
public class StudentController {

@Autowired
StudentService studentService;

@RequestMapping(method = RequestMethod.POST)
public Student createStudent(@RequestBody Student pStudent) {
return studentService.createStudent(pStudent);
}

@RequestMapping(value = "/{studentId}", method = RequestMethod.GET)
public Student getStudent(@PathVariable Integer studentId) {
return studentService.getStudent(studentId);
}

@RequestMapping(method = RequestMethod.PUT)
public Student updateStudent(@RequestBody Student pStudent) {
return studentService.updateStudent(pStudent);
}

@RequestMapping(value = "/{studentId}", method = RequestMethod.DELETE)
public String deleteStudent(@PathVariable Integer studentId) {
return studentService.deleetStudent(studentId);
}
}


Service :

import com.wfd.demo.entity.Student;

public interface StudentService {

Student createStudent(Student pStudent);
Student getStudent(Integer studentId);
Student updateStudent(Student pStudent);
String deleetStudent(Integer studentId);
}
------------------------------------------------------

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.wfd.demo.entity.Student;

/**
 * @author BayaReddy
 *
 */
@Service
public class StudentServiceImpl implements StudentService {

static Map<Integer, Student> map = new HashMap<>();

public Student createStudent(Student pStudent) {
map.put(pStudent.id, pStudent);
return pStudent;
}

public Student getStudent(Integer studentId) {
return map.get(studentId);
}

public Student updateStudent(Student pStudent) {
map.put(pStudent.id, pStudent);
return pStudent;
}

public String deleetStudent(Integer studentId) {
map.remove(studentId);
return studentId + " Deleted successfully";
}
}








Comments

Popular posts from this blog

Spring Boot + Maven + RestFul + JPA + Sqlite

View & Materialized View

JDBC with Sqlite