Swagger api integration with spring rest
project structure:
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.example</groupId>
<artifactId>swaggerex</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>swaggerex</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.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-web</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.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
SampleRest.java
@RestController
public class SampleRest {
final static Logger logger =
Logger.getLogger(SampleRest.class);
@GetMapping("/getData")
public String fromRest() {
return " from test servie";
}
@PostMapping(value="/createStudent",consumes="application/json"
,produces="application/json")
public ResponseEntity<Object>
acceptinput(@Valid @RequestBody Student data, BindingResult error) {
Map<String,String> err=new
HashMap<>();
if(error.hasErrors()) {
List<ObjectError> e=error.getAllErrors();
for(ObjectError ee:e) {
err.put(ee.getCode(), ee.getDefaultMessage());
}
return ResponseEntity.ok(err) ;
}
System.out.println("id=" +
data.getId());
System.out.println("name=" +
data.getName());
return ResponseEntity.ok(data) ;
}
@GetMapping("/getstudent")
public ResponseEntity<Object>
getstudentData() {
logger.info("method
called-getstudentData");
Student student=new Student();
student.setId("23");
student.setName("madhav");
logger.info("student name=" +
student.getName());
logger.info("method
called-getstudentData");
return ResponseEntity.ok(student) ;
}
@InitBinder
public void dataBinding(WebDataBinder binder) {
binder.addValidators(new Student());
}
}
student.java
public class Student
implements
Validator,Serializable{
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean supports(Class<?> clazz) {
// TODO Auto-generated method stub
return Student.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object student, Errors error) {
// TODO Auto-generated meth
ValidationUtils.rejectIfEmptyOrWhitespace(error, "id","id", "id cant be empty");
ValidationUtils.rejectIfEmptyOrWhitespace(error, "name","name", "name cant be empty");
}
}
SwaggerexApplication.java(main)
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages="com.controller")
@EnableSwagger2
public class SwaggerexApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerexApplication.class, args);
}
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select().apis(RequestHandlerSelectors.basePackage("com.controller"))
.paths(regex("/*.*"))
.build();
}
}
swagger api access:
No comments:
Post a Comment