方法有8个参数,大于7个授权
当我使用 sonar lint 扫描代码时,以下代码显示错误为方法有 8 个参数,大于 7 个授权"
When I am scanning code with sonar lint the following code shows the bug as "Method has 8 parameters, which is greater than 7 authorized"
@PutMapping("/something")
public List<SomeList> updateSomeThing(@PathVariable final SomeCode code,
@PathVariable final SomeId id,
@PathVariable final String testId,
@PathVariable final String itemId,
@RequestBody final List<Test> someList,
@RequestHeader("test") final String testHeader,
final HttpServletRequest request,
final SomeHeaders someHeaders)
注意:这是一个控制器方法,我们不能跳过任何参数
Note: This is a controller method we can not skip any parameters
仅供参考:Eclipse 将快速修复显示为 squid:S00107
FYI: Eclipse showing a quick fix as squid:S00107
有人知道如何解决这个错误吗?
Anybody have any idea how to resolve this bug?
推荐答案
这里有两件事要考虑.
- 您可以在 Sonar 中调整此规则并增加授权参数的数量.说它是 10 而不是默认值 (?) 7.
UPD:以下建议基于旧问题版本.它可能不再适用于新的问题上下文.
UPD: the advice below is based on the old question version. It might be not applicable to the new question context any more.
- 但通常你应该重新考虑你的方法接口.有很多参数意味着您的架构中可能有问题,并且单一责任原则可能会被打破.
- But generally you should reconsider your method interface. Having many arguments means that something can be wrong in your architecture and the Single responsibility principle might be broken.
在您的特定示例中,我希望您可以拥有一个聚合类 Order
:
Say in your particular example, I would expect, that you can have an aggregate class Order
:
public class Order {
private CountryCode countryCode;
private String orderId;
private User user;
private String orderId;
private String item;
private List<Person> persons;
private ShippingAddress address;
private PaymentMethod payment;
private Product product;
// ...
}
管理起来更合乎逻辑,而不是处理许多参数.然后您的问题将自动解决:
Which is much logical to manage instead of dealing with many parameters. Then your issues will be solved automatically:
@GetMapping
public void updateSomething(Order order) { ... }
相关文章