Skip to main content

Rest api - student - without DB



Pom.xml :-

 <!-- Dependencies for swagger -->
  <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>
 
 
 Application.java : -

 1. place bellow imports : -
 ---------------------------
 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;

2.add @EnableSwagger2 annotation in class level
3.add this code bellow main method
----------------------------
@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()));
}






-----------------------------------------------------------------------------------------------------------------------
public class Student {

 public String name;
 public int id;
}



----------------
import com.wfd.springDemo.model.Student;

@RestController
@RequestMapping("/newstudent")
public class StudentController {

static Student student;

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

@RequestMapping(method=RequestMethod.PUT)
public Student putStudent(@RequestBody Student student){
this.student=student;
return this.student;
}

@RequestMapping(method=RequestMethod.DELETE)
public Student deleteStudent(){
this.student=null;
return this.student;
}

@RequestMapping(method=RequestMethod.GET)
public Student getStudent(){
return this.student;
}
//Create - POST
//Update - Put
//Delete - delete
//Fetch  - Get
}


Comments

Popular posts from this blog

About Project

                                 About project About project : I am working on xyz project in which we are using xyz technologies This project consists multiple modules. Contribution : I am contributing to so and so modules Roles and responsibilities in Project  Involved in requirement gathering and followed the agile process Explain the roles and responsibilities mentioned in the resume

Java Training Content

                                               Java Training Contents :  Learn Computer Fundamentals -what is software Ref : https://www.techopedia.com/definition/4356/software -what is programming language, Functional Programming, OO programming Ref : https://www.geeksforgeeks.org/introduction-to-programming-languages/     : https://www.techopedia.com/definition/24815/programming-language : https://study.com/academy/lesson/what-is-programming-language-types-examples-quiz.html : https://www.youtube.com/watch?v=ifo76VyrBYo -Install JDK and set Java path -Hello world program Ref : -Compilation and interpretation Compilation : convert source code to executable code - interpreted : execute instruction line by line. -Features of Java - Platform Independent Ref : https://www.geeksforgeeks.org/java-platform-independent/ L...

Web-Service

Web Service :- a service offered by an electronic device to another electronic device, communicating with each other via the  World Wide Web Web services provide a common platform that allows multiple applications built on various programming languages to have the ability to communicate with each other Popular Web Services Protocols are: SOAP: SOAP is known as the Simple Object Access Protocol. SOAP was developed as an intermediate language so that applications built on various programming languages could talk quickly to each other and avoid the extreme development effort. SOAP can only work with XML format. SOAP cannot make use of REST since SOAP is a protocol and REST is an architectural pattern. SOAP was designed with a specification. It includes a WSDL file which has the required information on what the web service does in addition to the location of the web service. REST: REST stands for Representational State Transfer. REST is used to build Web serv...