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=

这里写图片描述

如图,只有正确的异常信息了。如何处理这个异常就是自己本身的逻辑了,这里只提供思路。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 隐形拉链不好拉怎么办 房东赶租客走怎么办 南京合租一间怎么办租房补贴 大学登错成绩怎么办 网游停服花了钱怎么办 北斗星x5油耗高怎么办 北斗星空调不凉怎么办 踏板摩托车费油怎么办 lol队友一直送怎么办 电脑信号无连接怎么办 电脑屏不全屏怎么办 电脑桌面不能显示全屏怎么办? 电脑黑屏无信号怎么办 笔记本电脑屏幕不显示怎么办 手机登录认证要怎么办 360手机丢失了怎么办 主播周年庆怎么办 学校选课系统进不去怎么办 选课系统关闭了怎么办 政府工作人员被威胁怎么办 报志愿忘记用户名怎么办 账号密码忘记了怎么办 商盟卡过期了怎么办 香港身份证掉了怎么办 简易注销有异议怎么办 左转红灯直行了怎么办 怀孕初期吃螃蟹怎么办 天下霸图打不过传闻怎么办 怀孕后期阴部痛怎么办 胎儿头大腿短怎么办 孕期大腿根疼怎么办 到工作单位催债怎么办 被讨债公司骚扰怎么办 要债人上门不走怎么办 阴阳师体力满了怎么办 狗吃了糯米怎么办 误给狗狗吃糯米怎么办 脸上长满了斑怎么办 突然满脸爆斑怎么办 熬夜脸上长斑怎么办 婴儿好感冒咳嗽怎么办