方法有8个参数,大于7个授权

2024-05-09Java开发问题
7

本文介绍了方法有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?

推荐答案

这里有两件事要考虑.

  1. 您可以在 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.

  1. 但通常你应该重新考虑你的方法接口.有很多参数意味着您的架构中可能有问题,并且单一责任原则可能会被打破.
  1. 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) { ... }

这篇关于方法有8个参数,大于7个授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13