gpt4 book ai didi

java - Spring @RequestMapping 值显式/隐式

转载 作者:行者123 更新时间:2023-12-01 14:03:12 25 4
gpt4 key购买 nike

在某些情况下,我看到有两种不同的方式在 @RequestMapping 中指定 uri:

  1. @RequestMapping(value="/somepath")

  2. @RequestMapping("/somepath")

后者通常在类级别使用。那么,省略 value 属性总是合法的吗?

最佳答案

Java Language Spec

It is legal to use single-element annotations for annotation types with multiple elements, so long as one element is named value, and all other elements have default values.

由于 @RequestMapping 具有定义的 value 属性,并且所有其他属性都有默认值(空数组),因此省略名称 value 的拼写始终是合法的 如果您没有为注释指定任何其他属性:

@RequestMapping("/somepath") // valid
@RequestMapping("/somepath", method = RequestMethod.GET) // INVALID!

关于java - Spring @RequestMapping 值显式/隐式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19176393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com