kotlin之springboot二
来源:互联网 发布:数据字典 样例 编辑:程序博客网 时间:2024/06/18 12:45
这次再上一篇基础,继续加入控制器,顺便演示个校验插件
首先gradle中加入jar包,多加个fastjson指定为HttpMessageConverters
compile 'org.springframework.boot:spring-boot-starter-freemarker'compile group: 'com.alibaba', name: 'fastjson', version: '1.2.24'compile 'com.github.shi0288:check:v1.0.1'
application-dev.properties加入:
#FREEMARKER (FreeMarkerAutoConfiguration)spring.freemarker.allow-request-override=falsespring.freemarker.allow-session-override=falsespring.freemarker.cache=truespring.freemarker.charset=UTF-8spring.freemarker.check-template-location=truespring.freemarker.content-type=text/htmlspring.freemarker.enabled=truespring.freemarker.expose-request-attributes=falsespring.freemarker.expose-session-attributes=falsespring.freemarker.expose-spring-macro-helpers=truespring.freemarker.prefer-file-system-access=truespring.freemarker.suffix=.ftlspring.freemarker.template-loader-path=classpath:/templates/spring.freemarker.settings.template_update_delay=0spring.freemarker.settings.default_encoding=UTF-8spring.freemarker.settings.classic_compatible=truespring.freemarker.settings.number_format=#spring.freemarker.order=1spring.freemarker.request-context-attribute=request
Application.kt:
package com.mcpimport com.alibaba.fastjson.serializer.SerializerFeatureimport com.alibaba.fastjson.support.config.FastJsonConfigimport com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverterimport org.springframework.boot.SpringApplicationimport org.springframework.boot.autoconfigure.SpringBootApplicationimport org.springframework.boot.autoconfigure.web.HttpMessageConvertersimport org.springframework.context.annotation.Bean/** * Created by shiqm on 2017-10-27. */@SpringBootApplicationopen class Application{ @Bean open fun fastJsonHttpMessageConverters(): HttpMessageConverters { val fastConverter = FastJsonHttpMessageConverter() val fastJsonConfig = FastJsonConfig() fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat) fastConverter.fastJsonConfig = fastJsonConfig return HttpMessageConverters(fastConverter) }}fun main(args: Array<String>) { SpringApplication.run(Application::class.java, *args)}
创建PageController
package com.mcp.controllerimport org.springframework.stereotype.Controllerimport org.springframework.web.bind.annotation.RequestMapping/** * Created by shiqm on 2017-10-27. */@Controllerclass PageController { @RequestMapping("login") fun login() { }}
创建AjaxController
package com.mcp.controllerimport org.springframework.web.bind.annotation.RequestMappingimport org.springframework.web.bind.annotation.RestController/** * Created by shiqm on 2017-10-27. */@RestControllerclass AjaxController { @RequestMapping("user") fun user() : Any{ return "My name is JACK!" }}
resources里创建模板文件夹templates
创建login.ftl
<!DOCTYPE html><html><head> <title>登录</title> <meta charset="UTF-8"></head><body><form action="/user"> <input type="text" name="username"><br> <input type="password" name="password"><br> <input type="submit" value="提交"></form></body></html>
运行Application,可以浏览页面
127.0.0.1:8080/login
之前我们加入了验证控件,现在修改AjaxController中user方法,加入校验规则,这里使username为字母或数字,长度不小于5,password长度不小于10
AjaxController修改如下:
package com.mcp.controllerimport com.mcp.validate.annotation.Checkimport org.springframework.web.bind.annotation.RequestMappingimport org.springframework.web.bind.annotation.RestController/** * Created by shiqm on 2017-10-27. */@RestControllerclass AjaxController { @RequestMapping("user") fun user( @Check(numOrLetter = true,length = 5) username: String, @Check(length = 10) password: String ): Any { return "My name is JACK!" }}
重新启动后测试:
127.0.0.1:8080/user?username=第三方个是的发生的&password=
已经报出异常,由此可以自己根据需求作校验了,校验控件有已经存在的校验方法,如果不合适,可以通过正则,或者自定义校验器,即可得到自己想要的。
这里再说一种处理异常的办法吧,我们可以做一个BaseController,对接口的所有异常信息进行监控和处理
创建BaseController
package com.mcp.controllerimport com.mcp.validate.exception.ValidateExceptionimport org.springframework.web.bind.annotation.ExceptionHandlerimport javax.servlet.http.HttpServletRequest/** * Created by shiqm on 2017-10-27. */open class BaseController { @ExceptionHandler(Exception::class) fun handleException(req: HttpServletRequest, ex: Exception): Any { return when (ex) { is ValidateException -> { ex.bindResult } else -> { ex.printStackTrace() "/login" } } }}
AjaxController继承此类
package com.mcp.controllerimport com.mcp.validate.annotation.Checkimport org.springframework.web.bind.annotation.RequestMappingimport org.springframework.web.bind.annotation.RestController/** * Created by shiqm on 2017-10-27. */@RestControllerclass AjaxController :BaseController(){ @RequestMapping("user") fun user( @Check(numOrLetter = true,length = 5) username: String, @Check(length = 10) password: String ): Any { return "My name is JACK!" }}
这时候重新启动,http://127.0.0.1:8080/user?username=sdf&password=
如图,只有正确的异常信息了。如何处理这个异常就是自己本身的逻辑了,这里只提供思路。
阅读全文
0 0
- kotlin之springboot二
- kotlin之springboot小试牛刀
- Kotlin编程之二
- Kotlin开发之旅《二》—Kotlin的基本语法
- Kotlin-40.kotlin调用Java之二(Call Java from Kotlin)
- Kotlin-44.Java调用kotlin之二(Call Kotlin from Java)
- Kotlin(二)
- SpringBoot构建微服务实战 之 Hello SpringBoot(二)
- 使用kotlin开发springboot应用
- SpringBoot初始教程之日志处理(二)
- Springboot(二) 接口调试之swagger
- SpringBoot系列之二_pom文件
- 菜鸟springboot学习之旅二
- SpringBoot配置属性之Server(二)
- Kotlin的Spring之旅(二):IOC控制反转
- 我的Kotlin For Android之路开篇(二)
- 我的Kotlin学习之旅(二)
- Kotlin集成 SpringBoot 混合Java库开发
- 前端面试题-滴滴二面
- java 常见单词收集
- Redis在windows下安装过程
- IntelliJ IDEA 安装问题解决
- Spring集成Mybatis时的常见报错
- kotlin之springboot二
- activiti定时器的时间用法
- Vue 2.0 的建议学习顺序
- Docker nginx+php+memcache安装遇到的几个小问题
- 同步与异步,阻塞与非阻塞
- Java应用OpenCV指南其四:OpenCV获取ROI区域、图像叠加
- 洛谷 P1278 单词游戏 【状压dp】
- 使用Spark实现相似度计算
- 图片裁切上传七牛云(base64上传)