优化下列代码: private final ApplicationProperty applicationProperty; private final DeveloperProperty developerProperty; @Autowired public PropertyController(ApplicationProperty applicationProperty, DeveloperProperty developerProperty) { this.applicationProperty = applicationProperty; this.developerProperty = developerProperty; } @GetMapping("/property") public Dict index() { return Dict.create().set("applicationProperty", applicationProperty).set("developerProperty", developerProperty); }
时间: 2024-01-06 20:06:29 浏览: 102
优化后的代码如下,主要是使用了Lombok的注解简化了代码:
```java
@RestController
@RequestMapping("/property")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PropertyController {
private final ApplicationProperty applicationProperty;
private final DeveloperProperty developerProperty;
@GetMapping
public Dict index() {
return Dict.create()
.set("applicationProperty", applicationProperty)
.set("developerProperty", developerProperty);
}
}
```
使用`@RestController`注解代替了`@Controller`和`@ResponseBody`,使用`@RequiredArgsConstructor`注解代替了构造方法,并且在注解中使用了Lombok的`@Autowired`注解,省略了构造方法中的`@Autowired`注解。
同时,将`@GetMapping`注解中的URL从`/property`修改为默认值,省略了URL参数。
阅读全文