Skip to content

Commit d1fa7be

Browse files
committed
add spring-boot-book-demo for api definition
1 parent c639374 commit d1fa7be

File tree

14 files changed

+469
-0
lines changed

14 files changed

+469
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**
5+
!**/src/test/**
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
30+
### VS Code ###
31+
.vscode/
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.7.10</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.aliyun.book</groupId>
12+
<artifactId>spring-boot-book-demo</artifactId>
13+
<version>1.0.0</version>
14+
<name>spring-boot-book-demo</name>
15+
<description>spring-boot-book-demo</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-validation</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.apache.commons</groupId>
32+
<artifactId>commons-lang3</artifactId>
33+
<version>3.11</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>io.springfox</groupId>
37+
<artifactId>springfox-swagger2</artifactId>
38+
<version>3.0.0</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>io.springfox</groupId>
42+
<artifactId>springfox-swagger-ui</artifactId>
43+
<version>3.0.0</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.projectlombok</groupId>
47+
<artifactId>lombok</artifactId>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-test</artifactId>
53+
<scope>test</scope>
54+
</dependency>
55+
</dependencies>
56+
57+
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-compiler-plugin</artifactId>
63+
<version>3.8.1</version>
64+
<configuration>
65+
<source>1.8</source>
66+
<target>1.8</target>
67+
<encoding>UTF-8</encoding>
68+
</configuration>
69+
</plugin>
70+
<plugin>
71+
<groupId>org.springframework.boot</groupId>
72+
<artifactId>spring-boot-maven-plugin</artifactId>
73+
<version>2.7.10</version>
74+
</plugin>
75+
</plugins>
76+
</build>
77+
78+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.aliyun.edas.book;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
6+
7+
@SpringBootApplication
8+
@EnableSwagger2
9+
public class BookStore {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(BookStore.class, args);
13+
}
14+
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.aliyun.edas.book.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
6+
import springfox.documentation.builders.PathSelectors;
7+
import springfox.documentation.builders.RequestHandlerSelectors;
8+
import springfox.documentation.spi.DocumentationType;
9+
import springfox.documentation.spring.web.plugins.Docket;
10+
11+
@Configuration
12+
@EnableWebMvc
13+
public class SwaggerConfig {
14+
15+
@Bean
16+
public Docket api() {
17+
return new Docket(DocumentationType.SWAGGER_2)
18+
.select()
19+
.apis(RequestHandlerSelectors.basePackage("com.aliyun.edas.book"))
20+
.paths(PathSelectors.any())
21+
.build();
22+
}
23+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.aliyun.edas.book.controller;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
import javax.servlet.http.HttpSession;
10+
11+
import com.aliyun.edas.book.model.BaseResult;
12+
import com.aliyun.edas.book.model.Book;
13+
import com.aliyun.edas.book.model.BookRequest;
14+
import org.springframework.http.HttpStatus;
15+
import org.springframework.http.MediaType;
16+
import org.springframework.http.ResponseEntity;
17+
import org.springframework.web.bind.annotation.DeleteMapping;
18+
import org.springframework.web.bind.annotation.GetMapping;
19+
import org.springframework.web.bind.annotation.PathVariable;
20+
import org.springframework.web.bind.annotation.PostMapping;
21+
import org.springframework.web.bind.annotation.RequestAttribute;
22+
import org.springframework.web.bind.annotation.RequestBody;
23+
import org.springframework.web.bind.annotation.RequestHeader;
24+
import org.springframework.web.bind.annotation.RequestMapping;
25+
import org.springframework.web.bind.annotation.RequestParam;
26+
import org.springframework.web.bind.annotation.RestController;
27+
28+
/**
29+
* @author jingfeng.xjf
30+
* @date 2023/9/5
31+
*/
32+
@RestController
33+
@RequestMapping(value = {"/book"})
34+
public class BookController {
35+
36+
@RequestMapping(value = "/getBook")
37+
public BaseResult<Book> getBook(String isbn) {
38+
return BaseResult.ok(new Book());
39+
}
40+
41+
@PostMapping(value = "/createBookJson", consumes = MediaType.APPLICATION_JSON_VALUE)
42+
public BaseResult<String> createBookJson(@RequestBody BookRequest book, String testParam) {
43+
return BaseResult.ok(book.getIsbn());
44+
}
45+
46+
@PostMapping(value = "/createBookFormPojo", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
47+
public BaseResult<Book> createBookFormPojo(BookRequest book) {
48+
return BaseResult.ok(new Book());
49+
}
50+
51+
@PostMapping(value = "/createBookFormPrivateType", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
52+
public String createBookFormPrivateType(@RequestParam String param1, @RequestParam Integer param2) {
53+
return "success";
54+
}
55+
56+
@PostMapping(value = "/createBookFormParam", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
57+
public String createBookFormParam(String param1, Integer param2) {
58+
return "success";
59+
}
60+
61+
@PostMapping(value = "/createBookFormWithoutConsumes")
62+
public BaseResult<String> createBookFormWithoutConsumes(Book book) {
63+
return BaseResult.ok(book.getIsbn());
64+
}
65+
66+
@GetMapping("/getBookPojo")
67+
public BaseResult<Book> getBookPojo(Book book) {
68+
return BaseResult.ok(new Book());
69+
}
70+
71+
@GetMapping(path = "/getBookPath/{isbn}")
72+
public BaseResult<Book> getBookPath(@PathVariable("isbn") String isbn) {
73+
return BaseResult.ok(new Book());
74+
}
75+
76+
@GetMapping(path = "/getBookHeader")
77+
public BaseResult<Book> getBookHeader(@RequestHeader("RequestId") String requestId) {
78+
return BaseResult.ok(new Book());
79+
}
80+
81+
@DeleteMapping("/deleteBook")
82+
public BaseResult<Boolean> deleteBook(String isbn) {
83+
return BaseResult.ok(true);
84+
}
85+
86+
@GetMapping("/listBooks")
87+
public BaseResult<List<Book>> listBooks() {
88+
return BaseResult.ok(Collections.emptyList());
89+
}
90+
91+
@GetMapping("/listBooksMap")
92+
public BaseResult<Map<String, Book>> listBooksMap() {
93+
return BaseResult.ok(Collections.emptyMap());
94+
}
95+
96+
@GetMapping("/testMapObj")
97+
public Map<String, Book> testMap() {
98+
return Collections.emptyMap();
99+
}
100+
101+
@GetMapping("/testMapString")
102+
public Map<String, String> testMapString() {
103+
return Collections.emptyMap();
104+
}
105+
106+
public static final String TEST_LITERAL = "test_literal";
107+
108+
@PostMapping(value = "/testRequestParamValue", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
109+
public BaseResult<String> testRequestParamValue(
110+
@RequestParam(name = "TestParam", required = false) String testParam,
111+
@RequestAttribute(name = "TestAttribute", required = false) String testAttribute,
112+
@RequestParam(name = TEST_LITERAL) String test_literal123
113+
) {
114+
return BaseResult.ok("success");
115+
}
116+
117+
@PostMapping(value = "/testInnerClass", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
118+
public ResponseEntity<String> testInnerClass(
119+
HttpServletRequest request, HttpServletResponse response, HttpSession session
120+
) {
121+
return new ResponseEntity<>("hello", HttpStatus.ACCEPTED);
122+
}
123+
124+
@PostMapping(value = {"/testMultiPath1", "testMultiPath"}, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
125+
public String testMultiPath() {
126+
return "success";
127+
}
128+
129+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.aliyun.edas.book.model;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* @author jingfeng.xjf
7+
* @date 2023/9/5
8+
*/
9+
@Data
10+
public class Author {
11+
private String name;
12+
private String email;
13+
private int age;
14+
private Book book;
15+
private IdCard idCard;
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.aliyun.edas.book.model;
2+
3+
/**
4+
* @author jingfeng.xjf
5+
* @date 2023/9/26
6+
*/
7+
public class AuthorRequest {
8+
9+
private String name;
10+
private String email;
11+
private int age;
12+
private IdCard idCard;
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.aliyun.edas.book.model;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* @author jingfeng.xjf
7+
* @date 2023/9/22
8+
*/
9+
@Data
10+
public class Bank {
11+
12+
private String name;
13+
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.aliyun.edas.book.model;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class BaseResult<T> {
7+
8+
private int code;
9+
10+
private String msg;
11+
12+
private T data;
13+
14+
public static <T> BaseResult<T> ok(T data) {
15+
return restResult(data, 200, "success");
16+
}
17+
18+
public static <T> BaseResult<T> restResult(T data, int code, String msg) {
19+
BaseResult<T> apiResult = new BaseResult<>();
20+
apiResult.setCode(code);
21+
apiResult.setData(data);
22+
apiResult.setMsg(msg);
23+
return apiResult;
24+
}
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.aliyun.edas.book.model;
2+
3+
import javax.validation.constraints.NotNull;
4+
5+
import lombok.Data;
6+
7+
/**
8+
* @author jingfeng.xjf
9+
* @date 2023/9/5
10+
*/
11+
@Data
12+
public class Book {
13+
14+
@NotNull
15+
private String isbn;
16+
17+
private String title;
18+
//private Author author;
19+
//
20+
//@Range(min = 0, max = 1000)
21+
//private int pages;
22+
//private float priceFloat;
23+
//private double price;
24+
//private boolean available;
25+
//private char format;
26+
//private Integer aInt;
27+
//private Float aFloat;
28+
//private Double aDouble;
29+
//private Boolean aBoolean;
30+
//private Character character;
31+
//private List<String> listTest;
32+
//private String[] stringArrayTest;
33+
//private int[] intArray;
34+
//private Integer[] integerArray;
35+
//private Map<String,String> mapTest;
36+
//private Set<String> setTest;
37+
//private List<Author> authors;
38+
//private Object anyTest;
39+
}

0 commit comments

Comments
 (0)