springboot controller获取请求参数的注解

springboot controller获取请求参数的注解,第1张

@PathVariable,@RequestParam,@RequestBody

1针对一些非必填的参数,可以使用required关键字来标识,同时必须设置默认值defaultValue,如getOrder方法中对price参数的获取:

@RequestParam(value = "price",required = false,defaultValue = "0") Integer price

2参数可以不配注解直接与Entity类绑定,但不支持json格式,只支持form-data和x- >在SpringBoot项目读取配置文件中读取值,我们会用到@Value注解来读取配置值,例如我们在配置文件中配置了服务器web域名为xxxcom的配置:
serverwebdomain=xxxcom
1
1
在代码中读取其配置项:
@Value("${serverwebdomain}")
private String domain;
1
2
1
2
如果这个配置项在配置文件中忘记配置或者有的场景中我们不需要配置这项的时候,在项目启动的时候就会报错。
报错信息如下:
Caused by: javalangIllegalArgumentException: Could not resolve placeholder 'serverwebdomain' in value "${serverwebdomain}"
at orgspringframeworkutilPropertyPlaceholderHelperparseStringValue(PropertyPlaceholderHelperjava:178)
at orgspringframeworkutilPropertyPlaceholderHelperreplacePlaceholders(PropertyPlaceholderHelperjava:124)
at orgspringframeworkcoreenvAbstractPropertyResolverdoResolvePlaceholders(AbstractPropertyResolverjava:239)
at orgspringframeworkcoreenvAbstractPropertyResolverresolveRequiredPlaceholders(AbstractPropertyResolverjava:210)
at orgspringframeworkcontextsupportPropertySourcesPlaceholderConfigurerlambda$processProperties$0(PropertySourcesPlaceholderConfigurerjava:175)
at orgspringframeworkbeansfactorysupportAbstractBeanFactoryresolveEmbeddedValue(AbstractBeanFactoryjava:918)
at orgspringframeworkbeansfactorysupportDefaultListableBeanFactorydoResolveDependency(DefaultListableBeanFactoryjava:1248)
at orgspringframeworkbeansfactorysupportDefaultListableBeanFactoryresolveDependency(DefaultListableBeanFactoryjava:1227)
at orgspringframeworkbeansfactoryannotationAutowiredAnnotationBeanPostProcessor$AutowiredFieldElementinject(AutowiredAnnotationBeanPostProcessorjava:640)
at orgspringframeworkbeansfactoryannotationInjectionMetadatainject(InjectionMetadatajava:119)
at orgspringframeworkbeansfactoryannotationAutowiredAnnotationBeanPostProcessorpostProcessProperties(AutowiredAnnotationBeanPostProcessorjava:399)
18 common frames omitted
1
2
3
4
5
6
7
8
9
10
11
12
13
1
2
3
4
5
6
7
8
9
10
11
12
13
这个时候就需要我们给@Value注解配置项给个默认值。
解决方法如下:
@Value("${serverwebdomain:#{null}}")
private String domain;
1
2
1
2
或者
@Value("${serverwebdomain:xxx}")
private String domain;
1
2
1
2
不过如果默认值我们要设置为null时,我们使用${serverwebdomain:null}时,拿到domain的默认值会是“null" null的字符串,所以这种情况下,我们使用 ${serverwebdomain:#{null}} 这种方式进行赋予默认值

您好,估计您的意思是想类似Eclipse的做法解释这个Annotation哪些有定义默认值,哪些没有。
这个可以Class clz = Validateclass
Method[] ms = clzgetDeclaredMethods();
然后判断ms[i]getDefaultValue()返回如果不是null就表示有定义默认值了。

import javalangannotationRetention;
import javalangannotationRetentionPolicy;
import javalangreflectInvocationTargetException;
import javalangreflectMethod;
@Retention(RetentionPolicyRUNTIME)
@interface GetView {
String Method();
String Value();
}
public class Temp {
@GetView(Method = "aa", Value = "bb")
public void test() {
Systemoutprintln("In test method");
}
public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException {
Temp temp = new Temp();
Method method = tempgetClass()getMethod("test");
Systemoutprintln(methodisAnnotationPresent(GetViewclass));
methodinvoke(temp);
GetView view = methodgetAnnotation(GetViewclass);
Systemoutprintln(viewMethod());
Systemoutprintln(viewValue());
}
}

定义:注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK15及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。

使用springMVC框架时,怎样在controller里获得Session。一、如果需要把某个命令对象放到session里面,完全可以去类上加@SessionAttributes,但这只针对请求对象。二、如果

此处的method可以取两个值,一个是RequestMethodGET,一个是RequestMethodPOST,
这样写的话你应该就能够看出是什么意思了把,就是请求该方法使用的模式,是get还是post


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/13384487.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-25
下一篇 2023-07-25

发表评论

登录后才能评论

评论列表(0条)

保存