spring boot——引數傳遞——設定請求方式——引數校驗——示例
阿新 • • 發佈:2022-04-09
控制器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.awaimai.web;
import org.hibernate.validator.constraints.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import java.util.Enumeration;
@RestController
@Validated
public class kzq
{
@RequestMapping (value= "/test4" , method=RequestMethod.GET)
public String test4( @Size (min = 2 ,max = 6 ,message = "姓名長度必須為2到6" ) @RequestParam ( "username" ) String name)
{
String s = name;
return s;
}
}
|
web訪問:
===================================================================
===================================================================
控制器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.awaimai.web;
import org.hibernate.validator.constraints.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import java.util.Enumeration;
@RestController
@Validated
public class kzq
{
@RequestMapping (value= "/test4" , method=RequestMethod.GET)
public String test4(
@Size (min = 2 ,max = 4 ,message = "姓名長度必須為2到4" ) @RequestParam ( "name" ) String name,
@Min (value = 3 ,message = "年齡最小為3" ) @Max (value = 5 ,message = "年齡最大為5" ) @RequestParam ( "age" ) Integer age)
{
return name+age;
}
}
|
web:訪問