@RequestMapping中value参数的值怎么能从注释的方法中获取

@RequestMapping中value参数的值怎么能从注释的方法中获取,第1张

此处的method可以取两个值,一个是RequestMethodGET,一个是RequestMethodPOST,

这样写的话你应该就能够看出是什么意思了把,就是请求该方法使用的模式,是get还是post

您好,估计您的意思是想类似Eclipse的做法解释这个Annotation哪些有定义默认值,哪些没有。

这个可以Class clz = Validateclass

Method[] ms = clzgetDeclaredMethods();

然后判断ms[i]getDefaultValue()返回如果不是null就表示有定义默认值了。

在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}} 这种方式进行赋予默认值

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());

}

}

//@PropertySource:读取属性文件@PropertySource(value={"classpath:conf/properties"})

@Configurationpublic class ConfigOfLifeCycle {

@Bean public Student student(){ return new Student();

}

}

以上就是关于@RequestMapping中value参数的值怎么能从注释的方法中获取全部的内容,包括:@RequestMapping中value参数的值怎么能从注释的方法中获取、java Anotation怎样取得每一个注解成员的默认值、java@value注解为什么直接取默认值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址:https://54852.com/web/9481858.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存