XuShengTao 6 лет назад
Родитель
Сommit
5fff2dea62

+ 71 - 51
pom.xml

@@ -31,16 +31,23 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-freemarker</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.hateoas</groupId>
+            <artifactId>spring-hateoas</artifactId>
+        </dependency>
         <!--<dependency>-->
             <!--<groupId>org.springframework.restdocs</groupId>-->
-            <!--<artifactId>spring-restdocs-mockmvc</artifactId>-->
+            <!--<artifactId>tot</artifactId>-->
             <!--<scope>test</scope>-->
         <!--</dependency>-->
+            <dependency>
+                <groupId>org.springframework.data</groupId>
+                <artifactId>spring-data-rest-hal-browser</artifactId>
+            </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-rest</artifactId>
         </dependency>
-
         <dependency>
             <groupId>com.h2database</groupId>
             <artifactId>h2</artifactId>
@@ -51,6 +58,14 @@
             <artifactId>lombok</artifactId>
             <optional>true</optional>
         </dependency>
+
+        <dependency>
+            <groupId>org.springframework.restdocs</groupId>
+            <artifactId>spring-restdocs-mockmvc</artifactId>
+            <version>2.0.0.RELEASE</version>
+            <scope>test</scope>
+        </dependency>
+
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
@@ -62,6 +77,11 @@
                 </exclusion>
             </exclusions>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
@@ -70,55 +90,55 @@
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
-            <!--<plugin>-->
-                <!--<groupId>org.asciidoctor</groupId>-->
-                <!--<artifactId>asciidoctor-maven-plugin</artifactId>-->
-                <!--<version>1.5.3</version>-->
-                <!--<executions>-->
-                    <!--<execution>-->
-                        <!--<id>generate-docs</id>-->
-                        <!--<phase>prepare-package</phase>-->
-                        <!--<goals>-->
-                            <!--<goal>process-asciidoc</goal>-->
-                        <!--</goals>-->
-                        <!--<configuration>-->
-                            <!--<backend>html</backend>-->
-                            <!--<doctype>book</doctype>-->
-                        <!--</configuration>-->
-                    <!--</execution>-->
-                <!--</executions>-->
-                <!--<dependencies>-->
-                    <!--<dependency>-->
-                        <!--<groupId>org.springframework.restdocs</groupId>-->
-                        <!--<artifactId>spring-restdocs-asciidoctor</artifactId>-->
-                        <!--<version>2.0.0.RELEASE</version>-->
-                    <!--</dependency>-->
-                <!--</dependencies>-->
-            <!--</plugin>-->
-            <!--<plugin>-->
-                <!--<artifactId>maven-resources-plugin</artifactId>-->
-                <!--<version>2.7</version>-->
-                <!--<executions>-->
-                    <!--<execution>-->
-                        <!--<id>copy-resources</id>-->
-                        <!--<phase>prepare-package</phase>-->
-                        <!--<goals>-->
-                            <!--<goal>copy-resources</goal>-->
-                        <!--</goals>-->
-                        <!--<configuration>-->
-                            <!--<outputDirectory>-->
-                                <!--${project.build.outputDirectory}/static/docs-->
-                            <!--</outputDirectory>-->
-                            <!--<resources>-->
-                                <!--<resource>-->
-                                    <!--<directory>-->
-                                        <!--${project.build.directory}/generated-docs-->
-                                    <!--</directory>                                </resource>-->
-                            <!--</resources>-->
-                        <!--</configuration>-->
-                    <!--</execution>-->
-                <!--</executions>-->
-            <!--</plugin>-->
+            <plugin>
+                <groupId>org.asciidoctor</groupId>
+                <artifactId>asciidoctor-maven-plugin</artifactId>
+                <version>1.5.3</version>
+                <executions>
+                    <execution>
+                        <id>generate-docs</id>
+                        <phase>prepare-package</phase>
+                        <goals>
+                            <goal>process-asciidoc</goal>
+                        </goals>
+                        <configuration>
+                            <backend>html</backend>
+                            <doctype>book</doctype>
+                        </configuration>
+                    </execution>
+                </executions>
+                <dependencies>
+                    <dependency>
+                        <groupId>org.springframework.restdocs</groupId>
+                        <artifactId>spring-restdocs-asciidoctor</artifactId>
+                        <version>2.0.0.RELEASE</version>
+                    </dependency>
+                </dependencies>
+            </plugin>
+            <plugin>
+                <artifactId>maven-resources-plugin</artifactId>
+                <version>2.7</version>
+                <executions>
+                    <execution>
+                        <id>copy-resources</id>
+                        <phase>prepare-package</phase>
+                        <goals>
+                            <goal>copy-resources</goal>
+                        </goals>
+                        <configuration>
+                            <outputDirectory>
+                                ${project.build.outputDirectory}/static/docs
+                            </outputDirectory>
+                            <resources>
+                                <resource>
+                                    <directory>
+                                        ${project.build.directory}/generated-docs
+                                    </directory>                                </resource>
+                            </resources>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
         </plugins>
     </build>
 

+ 0 - 22
src/main/all.ftl

@@ -1,22 +0,0 @@
-<!DOCTYPE html>
-<html>
-<head lang="en">
-    <meta charset="UTF-8" />
-    <title></title>
-</head>
-<body>
-${name}
-      <#if sex==1>
-      男
-      <#elseif sex==2>
-      女
-      <#else>
-      其他
-      </#if>
-
-      <#list userList as user>
-          ${user}
-      </#list>
-
-</body>
-</html>

+ 23 - 0
src/main/asciidoc/api-guide.adoc

@@ -0,0 +1,23 @@
+= Getting Started With Spring REST Docs
+
+This is an example output for a service running at http://localhost:8080:
+
+.request-curl
+include::{snippets}/index/curl-request.adoc[]
+
+.request-httpie
+include::{snippets}/index/httpie-request.adoc[]
+
+.request-body
+include::{snippets}/index/request-body.adoc[]
+
+.request
+include::{snippets}/index/http-request.adoc[]
+
+.response
+include::{snippets}/index/http-response.adoc[]
+
+.response-body
+include::{snippets}/index/response-body.adoc[]
+
+As you can see the format is very simple, and in fact you always get the same message.

+ 1 - 0
src/main/java/com/es/demo/DemoApplication.java

@@ -7,6 +7,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
 public class DemoApplication {
 
     public static void main(String[] args) {
+
         SpringApplication.run(DemoApplication.class, args);
     }
 

+ 22 - 0
src/main/java/com/es/demo/PageableConfig.java

@@ -0,0 +1,22 @@
+package com.es.demo;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer;
+
+/**
+ * @ClassName PageableConfig
+ * @PackageName com.es.demo
+ * @Author sheen
+ * @Date 2019/12/24
+ * @Version 1.0
+ * @Description //TODO
+ **/
+@Configuration
+public class PageableConfig {
+
+    @Bean
+    PageableHandlerMethodArgumentResolverCustomizer pageableResolverCustomizer() {
+        return pageableResolver -> pageableResolver.setOneIndexedParameters(true);
+    }
+}

+ 45 - 14
src/main/java/com/es/demo/WebRestfulController.java

@@ -3,6 +3,7 @@ package com.es.demo;
 import com.es.demo.back.ChoiceQuestion;
 import com.es.demo.back.TQRepository;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Pageable;
@@ -11,6 +12,7 @@ import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
 import org.springframework.data.elasticsearch.core.query.GetQuery;
 import org.springframework.data.elasticsearch.core.query.IndexQuery;
 import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
+import org.springframework.util.Assert;
 import org.springframework.web.bind.annotation.*;
 import sun.security.util.Length;
 
@@ -30,11 +32,12 @@ import java.util.stream.Stream;
 @RequestMapping("/rest")
 public class WebRestfulController {
     private ElasticsearchOperations elasticsearchOperations;
-    private final TQRepository tqRepository;
     @Autowired
-    public WebRestfulController(ElasticsearchOperations elasticsearchOperations, TQRepository tqRepository) {
+    @Qualifier("TQRepositoryImp")
+    private TQRepository tqRepository;
+    @Autowired
+    public WebRestfulController(ElasticsearchOperations elasticsearchOperations) {
         this.elasticsearchOperations = elasticsearchOperations;
-        this.tqRepository = tqRepository;
     }
 
     @PostMapping("/tq")
@@ -67,16 +70,31 @@ public class WebRestfulController {
     public String saveAll(@RequestParam String chapeter,@RequestBody String content) {
         try {
             Map<Integer,ChoiceQuestion> questions=new LinkedHashMap<>();
+            final int answerStartPos = content.lastIndexOf("## 参考答案与解析");
+            final int choiceStartPos = content.lastIndexOf("## 单项选择题");
+            final int completionStartPos = content.lastIndexOf("## 填空题");
+            final int shortAnswerStartPos = content.lastIndexOf("## 简答题");
 
-            SetQuestion(chapeter, content, questions);
+            int choiceStartPosEnd=content.length();
+            if(answerStartPos!=-1){choiceStartPosEnd=answerStartPos;}
+            if(shortAnswerStartPos!=-1){choiceStartPosEnd=shortAnswerStartPos;}
+            if(completionStartPos!=-1){choiceStartPosEnd=completionStartPos;}
 
-            final int answerStartPos = content.lastIndexOf("## 参考答案与解析");
-            content=content.substring(answerStartPos);
-            SetAnswer(chapeter, content, questions);
+            String choiceQuestionContent=content.substring(choiceStartPos,choiceStartPosEnd);
+//            return choiceQuestionContent;
+            Assert.isTrue(choiceQuestionContent.length()>8,"不存在题目!");
+            SetQuestion(chapeter, choiceQuestionContent, questions);
+
+
+            String answerContent=content.substring(answerStartPos);
+            SetAnswer(chapeter, answerContent, questions);
 
             System.out.println(questions.toString());
             questions.forEach((k,v)->{
-                saveChoiceQuestion(v);
+                if(v.questionIsLegal()){
+                    saveChoiceQuestion(v);
+                }
+
             });
             return String.valueOf(questions.size());
         }catch (Exception e){
@@ -85,12 +103,14 @@ public class WebRestfulController {
 
     }
 
+
     private void SetQuestion(String chapeter,String content, Map<Integer, ChoiceQuestion> questions) {
         int num=1;
         String type="choice";
         while (true){
             String startTag="\n"+num+".";
-            int startPos=content.indexOf(startTag);
+            String startTagOr="\n"+num+"\\.";
+            int startPos= content.contains(startTag) ?content.indexOf(startTag):content.indexOf(startTagOr);
             if(startPos==-1){ break;}
             content = content.substring(startPos);
             int endPos=getEndPos(num,content);
@@ -137,7 +157,9 @@ public class WebRestfulController {
     private void SetAnswer(String chapeter,String content, Map<Integer, ChoiceQuestion> questions) {
         int num=1;
         while (true){
-            int startPos=content.indexOf("\n"+num+".");
+            String startTag="\n"+num+".";
+            String startTagOr="\n"+num+"\\.";
+            int startPos= content.contains(startTag) ?content.indexOf(startTag):content.indexOf(startTagOr);
             if(startPos==-1){ break;}
             content = content.substring(startPos);
             int endPos=getEndPos(num,content);
@@ -146,14 +168,23 @@ public class WebRestfulController {
             int answerStart=solve.indexOf("【答案】");
             int keyPointsStart=solve.indexOf("【考点】");
             int analysisStart=solve.indexOf("【解析】");
-            questions.get(id).initAnswer(   solve.substring(answerStart,keyPointsStart).trim(),
-                                            solve.substring(keyPointsStart,analysisStart).trim(),
-                                            solve.substring(analysisStart,solve.length()).trim());
+            Assert.isTrue(answerStart>0,startTag+" answerStart!=-1");
+            Assert.isTrue(keyPointsStart>0,startTag+" keyPointsStart!=-1");
+            Assert.isTrue(analysisStart>0,startTag+" analysisStart!=-1");
+            final ChoiceQuestion choiceQuestion = questions.get(id);
+            if(choiceQuestion!=null) {
+                choiceQuestion.initAnswer(solve.substring(answerStart, keyPointsStart).trim(),
+                        solve.substring(keyPointsStart, analysisStart).trim(),
+                        solve.substring(analysisStart, solve.length()).trim());
+            }
             num++;
         }
     }
     private int getEndPos(int num, String content){
-        int nextQ=content.indexOf("\n"+(num+1)+".");
+
+        String startTag="\n"+(num+1)+".";
+        String startTagOr="\n"+(num+1)+"\\.";
+        int nextQ= content.contains(startTag) ?content.indexOf(startTag):content.indexOf(startTagOr);
         int nextC=content.indexOf("\n## ");
         if(nextQ==-1&&nextC==-1){
             return content.length();

+ 22 - 1
src/main/java/com/es/demo/back/ChoiceQuestion.java

@@ -1,13 +1,17 @@
 package com.es.demo.back;
 
+import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import org.springframework.data.annotation.Id;
+import org.springframework.data.annotation.LastModifiedDate;
 import org.springframework.data.elasticsearch.annotations.Document;
 
+import java.util.Date;
 import java.util.HashMap;
+import java.util.Set;
 
 /**
  * @ClassName ChoiceQuestion
@@ -21,20 +25,35 @@ import java.util.HashMap;
 @NoArgsConstructor
 @Document(indexName = "test_question", type = "test_type")
 public class ChoiceQuestion {
+
     @Id
     private Integer id;
+
     @JsonProperty(value = "type")
     private String type;
+
     @JsonProperty(value = "stem")
     private String stem;
+
     @JsonProperty(value = "options")
     private HashMap options;
+
     @JsonProperty(value = "answer")
     private String answer;
+
     @JsonProperty(value = "key_points")
     private String key_points;
+
     @JsonProperty(value = "analysis")
     private String analysis;
+
+    @JsonProperty(value = "tags")
+    private Set<String> tags;
+
+    @JsonProperty(value = "lastModified")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    private Date lastModifiedTime;
+
     @JsonProperty(value = "tq_id")
     public Integer getTqId() {
         return id;
@@ -51,7 +70,9 @@ public class ChoiceQuestion {
         this.key_points=key_points;
         this.analysis=analysis;
     }
-
+    public boolean questionIsLegal() {
+        return this.getStem().length()>4||this.getOptions().size()>2;
+    }
     @Override
     public String toString() {
         return "\n\nChoiceQuestion{" +

+ 36 - 0
src/main/java/com/es/demo/back/ChoiceQuestionController.java

@@ -0,0 +1,36 @@
+package com.es.demo.back;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler;
+import org.springframework.data.rest.webmvc.RepositoryRestController;
+import org.springframework.data.web.PagedResourcesAssembler;
+import org.springframework.hateoas.server.RepresentationModelAssembler;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.RequestEntity;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks;
+/**
+ * @ClassName ChoiceQuestionController
+ * @PackageName com.es.demo.back
+ * @Author sheen
+ * @Date 2019/12/26
+ * @Version 1.0
+ * @Description //TODO
+ **/
+@RepositoryRestController
+@RequestMapping("/tq")
+public class ChoiceQuestionController {
+    @Autowired
+    @Qualifier("TQRepositoryImp")
+    private TQRepository tqRepository;
+    @Autowired
+    private RepositoryEntityLinks entityLinks;
+    @RequestMapping(value = "/{tqId}",method= RequestMethod.PUT, produces = "application/hal+json")
+    public @ResponseBody ResponseEntity<?>
+            updateTQ(@PathVariable Integer tqId, @RequestBody ChoiceQuestion person,PersistentEntityResourceAssembler resourcesAssembler) {
+        final ChoiceQuestion saved = tqRepository.save(person);
+        return new ResponseEntity(resourcesAssembler.toFullResource(saved), HttpStatus.OK);
+    }
+}

+ 8 - 0
src/main/java/com/es/demo/back/TQRepository.java

@@ -5,7 +5,9 @@ import org.springframework.data.domain.Pageable;
 import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
 import org.springframework.data.rest.core.annotation.RepositoryRestResource;
 
+import java.util.Collection;
 import java.util.Optional;
+import java.util.Set;
 
 /**
  * @ClassName TQRepository
@@ -21,6 +23,12 @@ public interface  TQRepository extends ElasticsearchRepository<ChoiceQuestion,In
     Page<ChoiceQuestion> findAllByIdBetween( Pageable pageable,Integer start, Integer end);
     Page<ChoiceQuestion> findByStemLike(String content,Pageable pageable);
     int countAllByIdBetween(Integer id, Integer id2);
+    int deleteAllByIdBetween(Integer id, Integer id2);
     void deleteById(Integer integer);
     Optional<ChoiceQuestion> findById(Integer integer);
+
+    Page<ChoiceQuestion> findAllByTagsContaining(String tag, Pageable pageable);
+    Page<ChoiceQuestion> findAllByTagsNotIn(Collection<String> tags, Pageable pageable);
+    @Override
+    <S extends ChoiceQuestion> S save(S s);
 }

+ 169 - 0
src/main/java/com/es/demo/back/TQRepositoryImp.java

@@ -0,0 +1,169 @@
+package com.es.demo.back;
+
+import org.elasticsearch.index.query.QueryBuilder;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.elasticsearch.core.query.SearchQuery;
+import org.springframework.data.rest.core.annotation.RepositoryRestResource;
+import org.springframework.stereotype.Repository;
+
+import java.util.Collection;
+import java.util.Date;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * @ClassName TQRepositoryImp
+ * @PackageName com.es.demo.back
+ * @Author sheen
+ * @Date 2019/12/26
+ * @Version 1.0
+ * @Description //TODO
+ **/
+@Repository("TQRepositoryImp")
+
+public class TQRepositoryImp  implements TQRepository {
+
+    @Autowired
+    @Qualifier("TQRepository") // inject Spring implementation here
+    private TQRepository TQRepository;
+
+    @Override
+    public Page<ChoiceQuestion> findAll(Pageable pageable) {
+        return TQRepository.findAll(pageable);
+    }
+
+    @Override
+    public Page<ChoiceQuestion> findAllByIdBetween(Pageable pageable, Integer start, Integer end) {
+        return TQRepository.findAllByIdBetween(pageable, start, end);
+    }
+
+    @Override
+    public Page<ChoiceQuestion> findByStemLike(String content, Pageable pageable) {
+        return TQRepository.findByStemLike(content, pageable);
+    }
+
+    @Override
+    public int countAllByIdBetween(Integer id, Integer id2) {
+        return TQRepository.countAllByIdBetween(id, id2);
+    }
+
+    @Override
+    public int deleteAllByIdBetween(Integer id, Integer id2) {
+        return TQRepository.deleteAllByIdBetween(id,id2);
+    }
+
+    @Override
+    public void deleteById(Integer integer) {
+        TQRepository.deleteById(integer);
+    }
+
+    @Override
+    public Optional<ChoiceQuestion> findById(Integer integer) {
+        return TQRepository.findById(integer);
+    }
+
+    @Override
+    public Page<ChoiceQuestion> findAllByTagsContaining(String tags, Pageable pageable) {
+        return TQRepository.findAllByTagsContaining(tags,pageable);
+    }
+
+    @Override
+    public <S extends ChoiceQuestion> S save(S s) {
+        s.setLastModifiedTime(new Date());
+        return TQRepository.save(s);
+    }
+
+    @Override
+    public <S extends ChoiceQuestion> S index(S s) {
+        return TQRepository.index(s);
+    }
+
+    @Override
+    public <S extends ChoiceQuestion> S indexWithoutRefresh(S s) {
+        return TQRepository.indexWithoutRefresh(s);
+    }
+
+    @Override
+    public Iterable<ChoiceQuestion> search(QueryBuilder queryBuilder) {
+        return TQRepository.search(queryBuilder);
+    }
+
+    @Override
+    public Page<ChoiceQuestion> search(QueryBuilder queryBuilder, Pageable pageable) {
+        return TQRepository.search(queryBuilder, pageable);
+    }
+
+    @Override
+    public Page<ChoiceQuestion> search(SearchQuery searchQuery) {
+        return TQRepository.search(searchQuery);
+    }
+
+    @Override
+    public Page<ChoiceQuestion> searchSimilar(ChoiceQuestion choiceQuestion, String[] strings, Pageable pageable) {
+        return TQRepository.searchSimilar(choiceQuestion, strings, pageable);
+    }
+
+    @Override
+    public void refresh() {
+        TQRepository.refresh();
+    }
+
+    @Override
+    public Class<ChoiceQuestion> getEntityClass() {
+        return TQRepository.getEntityClass();
+    }
+
+    @Override
+    public Iterable<ChoiceQuestion> findAll(Sort sort) {
+        return TQRepository.findAll(sort);
+    }
+
+    @Override
+    public <S extends ChoiceQuestion> Iterable<S> saveAll(Iterable<S> iterable) {
+        return TQRepository.saveAll(iterable);
+    }
+
+    @Override
+    public boolean existsById(Integer integer) {
+        return TQRepository.existsById(integer);
+    }
+
+    @Override
+    public Iterable<ChoiceQuestion> findAll() {
+        return TQRepository.findAll();
+    }
+
+    @Override
+    public Iterable<ChoiceQuestion> findAllById(Iterable<Integer> iterable) {
+        return TQRepository.findAllById(iterable);
+    }
+
+    @Override
+    public long count() {
+        return TQRepository.count();
+    }
+
+    @Override
+    public void delete(ChoiceQuestion choiceQuestion) {
+        TQRepository.delete(choiceQuestion);
+    }
+
+    @Override
+    public void deleteAll(Iterable<? extends ChoiceQuestion> iterable) {
+        TQRepository.deleteAll(iterable);
+    }
+
+    @Override
+    public void deleteAll() {
+        TQRepository.deleteAll();
+    }
+
+    @Override
+    public Page<ChoiceQuestion> findAllByTagsNotIn(Collection<String> tags, Pageable pageable) {
+        return TQRepository.findAllByTagsNotIn(tags, pageable);
+    }
+}

+ 0 - 22
src/main/resources/all.ftl

@@ -1,22 +0,0 @@
-<!DOCTYPE html>
-<html>
-<head lang="en">
-    <meta charset="UTF-8" />
-    <title></title>
-</head>
-<body>
-${name}
-      <#if sex==1>
-      男
-      <#elseif sex==2>
-      女
-      <#else>
-      其他
-      </#if>
-
-      <#list userList as user>
-          ${user}
-      </#list>
-
-</body>
-</html>

+ 2 - 1
src/main/resources/application.properties

@@ -9,4 +9,5 @@ spring.freemarker.request-context-attribute=request
 spring.freemarker.suffix=.ftl
 spring.data.rest.basePath=/api
 spring.data.rest.returnBodyOnUpdate=true
-spring.data.rest.returnBodyOnCreate=true
+spring.data.rest.returnBodyOnCreate=true
+spring.data.web.pageable.one-indexed-parameters=true

+ 41 - 2
src/test/java/com/es/demo/DemoApplicationTests.java

@@ -1,13 +1,52 @@
 package com.es.demo;
 
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.MediaType;
+import org.springframework.restdocs.JUnitRestDocumentation;
+import org.springframework.restdocs.RestDocumentationContextProvider;
+import org.springframework.restdocs.RestDocumentationExtension;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
 
+import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
+import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders;
+import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
+import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
+import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
+import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
+import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
+import static org.springframework.restdocs.snippet.Attributes.*;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
+import org.springframework.web.context.WebApplicationContext;
+
+@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
 @SpringBootTest
 class DemoApplicationTests {
-
+    private MockMvc mockMvc;
+    @BeforeEach
+    public void setUp(WebApplicationContext webApplicationContext,
+                      RestDocumentationContextProvider restDocumentation) {
+        this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
+                .apply(documentationConfiguration(restDocumentation))
+                .build();
+    }
     @Test
-    void contextLoads() {
+    void contextLoads() throws Exception{
+        this.mockMvc.perform(get("/api/tq?page=1&size=2").accept(MediaType.APPLICATION_JSON))
+                .andExpect(status().isOk())
+                .andDo(document("index",requestParameters(
+                        parameterWithName("page").description("从1开始的页号"),
+                        parameterWithName("size").description("每页大小")
+                        )));
     }
 
 }