尝试在IntelliJ中进行结构替换

时间:2019-02-13 02:20:00

标签: intellij-idea

我想将我的注释从@RequestMapping替换为@ GetMapping,@ PutMapping ...注释。当我查看IntelliJ中的“结构查找/替换”时,看起来可以完成这项工作。

我尝试在搜索中添加以下内容

@RequestMapping($ key $ = $ value $) 在键上添加了一个过滤器。 text = method。

现在,我想从值(RequestMethod.GET)中提取单词。 (期)。然后在替换中添加

@ [Word(TitleCase)]映射([除搜索结果中提取的键值以外的所有内容]

还无法弄清楚该如何解决。希望知道是否无法完成此操作,或者对如何执行此操作有任何建议。看了SO上的其他一些问题,但没有找到任何可以帮助的问题。在这种情况下,大多数答案是使用正则表达式。

之前:

@RequestMapping(
    value = "/channels/{channel_name}",
    method = RequestMethod.POST,
    produces = MediaType.APPLICATION_JSON_VALUE,
    consumes = MediaType.APPLICATION_JSON_VALUE)
public Channel updateChannel(
    @PathVariable("channel_name") String channelName,
    @Valid @RequestBody Channel channel) {
  return channelService.updateChannel(channelName, channel);
}

@RequestMapping(
    value = "/channels/{channel_name}",
    method = RequestMethod.DELETE,
    produces = MediaType.APPLICATION_JSON_VALUE)
public Channel deleteChannel(
    @PathVariable("channel_name") String channelName) {
  return channelService.deleteChannel(channelName);
}

之后

@PostMapping(value = "/channels/{channel_name}",
    produces = MediaType.APPLICATION_JSON_VALUE,
    consumes = MediaType.APPLICATION_JSON_VALUE)
public Channel updateChannel(
    @PathVariable("channel_name") String channelName,
    @Valid @RequestBody Channel channel) {
  return channelService.updateChannel(channelName, channel);
}

@DeleteMapping(
    value = "/channels/{channel_name}",
    produces = MediaType.APPLICATION_JSON_VALUE)
public Channel deleteChannel(
    @PathVariable("channel_name") String channelName) {
  return channelService.deleteChannel(channelName);
}

1 个答案:

答案 0 :(得分:0)

我会用正则表达式来处理这个问题

  1. RequestMethod.(.)(.+)(?=,)替换为RequestMethod.\U$1\L$2\L would turn the text into lowercase。)
  2. @RequestMapping\((\s+)(.+)(\s+?)(.+)RequestMethod.(.+?),替换为@$5Mapping\($1$2$3

然后简化此替换链:

@RequestMapping\((\s+)(.+)(\s+?)(.+)RequestMethod.(\S)(.+?),替换为@\U$5\L$6\EMapping\($1$2

更新:注意,无论在value行还是独立行中都未指定第一个参数@Mapping

  • 如果在@Mapping行中需要它,请将@RequestMapping\((\s+)(.+)(\s+?)(.+)RequestMethod.(\S)(.+?),\s替换为@\U$5\L$6\EMapping\($2$3
  • 如果需要将其替换为独立行,请将@RequestMapping\((\s+)(.+)(\s+?)(.+)RequestMethod.(\S)(.+?),替换为@\U$5\L$6\EMapping\($1$2