kotlin之springboot二

这次再上一篇基础,继续加入控制器,顺便演示个校验插件

首先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=false
spring.freemarker.allow-session-override=false
spring.freemarker.cache=true
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.enabled=true
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.prefer-file-system-access=true
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.settings.template_update_delay=0
spring.freemarker.settings.default_encoding=UTF-8
spring.freemarker.settings.classic_compatible=true
spring.freemarker.settings.number_format=#
spring.freemarker.order=1
spring.freemarker.request-context-attribute=request

Application.kt:

package com.mcp

import com.alibaba.fastjson.serializer.SerializerFeature
import com.alibaba.fastjson.support.config.FastJsonConfig
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.web.HttpMessageConverters
import org.springframework.context.annotation.Bean

/**
 * Created by shiqm on 2017-10-27.
 */

@SpringBootApplication
open 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.controller

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping

/**
 * Created by shiqm on 2017-10-27.
 */

@Controller
class PageController {

    @RequestMapping("login")
    fun login() { }

}

创建AjaxController

package com.mcp.controller

import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * Created by shiqm on 2017-10-27.
 */
@RestController
class 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.controller

import com.mcp.validate.annotation.Check
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * Created by shiqm on 2017-10-27.
 */
@RestController
class 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.controller

import com.mcp.validate.exception.ValidateException
import org.springframework.web.bind.annotation.ExceptionHandler
import 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.controller

import com.mcp.validate.annotation.Check
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * Created by shiqm on 2017-10-27.
 */
@RestController
class 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值