Explorar el Código

1.引入Swagger-ui;
2.添加Thymeleaf模板引擎;
3.修改配置文件。

Yumin hace 6 años
padre
commit
92d317af76

+ 19 - 0
pom.xml

@@ -44,6 +44,12 @@
             <scope>test</scope>
         </dependency>
 
+        <!-- Thymeleaf 模板引擎 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-thymeleaf</artifactId>
+        </dependency>
+
         <!-- MySQL 数据库依赖 -->
         <dependency>
             <groupId>mysql</groupId>
@@ -65,6 +71,19 @@
             <version>1.2.38</version>
         </dependency>
 
+        <!-- Swagger2 -->
+        <dependency>
+            <groupId>io.springfox</groupId>
+            <artifactId>springfox-swagger-ui</artifactId>
+            <version>2.7.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>io.springfox</groupId>
+            <artifactId>springfox-swagger2</artifactId>
+            <version>2.7.0</version>
+        </dependency>
+
         <!-- lombok -->
         <dependency>
             <groupId>org.projectlombok</groupId>

+ 37 - 0
src/main/java/cn/minbb/edu/Swagger2.java

@@ -0,0 +1,37 @@
+package cn.minbb.edu;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import springfox.documentation.builders.ApiInfoBuilder;
+import springfox.documentation.builders.PathSelectors;
+import springfox.documentation.builders.RequestHandlerSelectors;
+import springfox.documentation.service.ApiInfo;
+import springfox.documentation.service.Contact;
+import springfox.documentation.spi.DocumentationType;
+import springfox.documentation.spring.web.plugins.Docket;
+import springfox.documentation.swagger2.annotations.EnableSwagger2;
+
+@Configuration
+@EnableSwagger2
+public class Swagger2 {
+
+    @Bean
+    public Docket createRestApi() {
+        return new Docket(DocumentationType.SWAGGER_2)
+                .apiInfo(apiInfo())
+                .select()
+                .apis(RequestHandlerSelectors.basePackage("cn.minbb.edu.controller.rest"))
+                .paths(PathSelectors.any())
+                .build();
+    }
+
+    private ApiInfo apiInfo() {
+        return new ApiInfoBuilder()
+                .title("Spring Boot 中使用 Swagger2 构建RESTful APIs")
+                .description("主页:https://www.minbb.cn/")
+                .termsOfServiceUrl("https://www.minbb.cn/")
+                .contact(new Contact("Yumin", "https://www.minbb.cn/", "yumin@minbb.cn"))
+                .version("1.0")
+                .build();
+    }
+}

+ 18 - 0
src/main/java/cn/minbb/edu/controller/web/MainController.java

@@ -1,17 +1,35 @@
 package cn.minbb.edu.controller.web;
 
+import cn.minbb.edu.service.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
 
 @Controller
 @RequestMapping(value = "")
 public class MainController {
 
+    private UserService userService;
+
+    @Autowired
+    public MainController(UserService userService) {
+        this.userService = userService;
+    }
+
     @GetMapping(value = "")
     @ResponseBody
     public String main() {
         return "EDUServer Running...";
     }
+
+    @GetMapping(value = {"login", "sign-in"})
+    public ModelAndView signIn(ModelAndView modelAndView, HttpServletRequest request) {
+        modelAndView.setViewName("sign-in");
+        return modelAndView;
+    }
 }

+ 1 - 1
src/main/java/cn/minbb/edu/service/impl/UserServiceImpl.java

@@ -9,7 +9,7 @@ import org.springframework.stereotype.Service;
 @Service
 public class UserServiceImpl implements UserService {
 
-    private final UserRepository userRepository;
+    private UserRepository userRepository;
 
     @Autowired
     public UserServiceImpl(UserRepository userRepository) {

+ 1 - 0
src/main/resources/application-dev.properties

@@ -1,5 +1,6 @@
 # ¿ª·¢»·¾³
 server.servlet.context-path=/
 # MySQL
+spring.datasource.url=jdbc:mysql://127.0.0.1:3306/edu?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
 spring.datasource.username=
 spring.datasource.password=

+ 6 - 0
src/main/resources/application-pro.properties

@@ -0,0 +1,6 @@
+# Éú²ú»·¾³
+server.servlet.context-path=/
+# MySQL
+spring.datasource.url=jdbc:mysql://www.minbb.cn:3306/edu?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
+spring.datasource.username=Yumin
+spring.datasource.password=Wang19970305

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

@@ -2,11 +2,15 @@
 spring.profiles.active=pro
 server.port=80
 server.servlet.context-path=/
-spring.datasource.url=jdbc:mysql://127.0.0.1:3306/edu?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
+spring.datasource.url=
 spring.datasource.username=
 spring.datasource.password=
 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 spring.datasource.sql-script-encoding=UTF-8
 spring.jpa.show-sql=false
 spring.jpa.properties.hibernate.hbm2ddl.auto=update
-spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
+spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
+# Ä£°åÒýÇæ
+spring.thymeleaf.mode=HTML
+spring.thymeleaf.cache=false
+spring.thymeleaf.encoding=UTF-8

+ 73 - 0
src/main/resources/templates/sign-in.html

@@ -0,0 +1,73 @@
+<!DOCTYPE html>
+<html lang="zh-CN"
+      xmlns="http://www.w3.org/1999/html"
+      xmlns:th="http://www.thymeleaf.org">
+<head>
+    <meta charset="utf-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1">
+    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
+    <title>用户登录</title>
+
+    <!-- Bootstrap -->
+    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
+
+    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
+    <!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
+    <!--[if lt IE 9]>
+    <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
+    <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
+    <![endif]-->
+</head>
+<body>
+<div class="container">
+    <div class="row">
+        <h1>用户登录</h1>
+    </div>
+
+    <div class="row">
+        <div class="col-sm-12 col-md-6 col-lg-6">
+            <div class="form-group">
+                <input id="username" type="text" class="form-control" placeholder="用户名"/>
+            </div>
+
+            <div class="form-group">
+                <input id="password" type="password" class="form-control" placeholder="密码"/>
+            </div>
+
+            <button type="button" class="btn btn-default" onclick="login();">登录</button>
+        </div>
+
+        <div class="col-sm-12 col-md-6 col-lg-6">
+
+        </div>
+    </div>
+</div>
+
+<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
+<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
+<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
+<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
+
+<script>
+    function login() {
+        $.ajax({
+            url: '/user/login?username=' + $('#username').val() + '&password=' + $('#password').val(),
+            type: 'POST',
+            data: null,
+            dataType: "text",
+            async: false,
+            cache: false,
+            contentType: false,
+            processData: false,
+            success: function (data) {
+                alert(data)
+            },
+            error: function (data) {
+                console.log(data);
+            }
+        });
+    }
+</script>
+</body>
+</html>