【问题标题】:Annotation RequestMapping() giving the error注释 RequestMapping() 给出错误
【发布时间】:2018-08-06 13:16:03
【问题描述】:

我从 Spring Boot 开始并尝试提供 Rest 服务。 我正在编写一个控制器,其中有 3 种方法的 RequestMappings。 其中两个工作正常,而 thirl 注释在编写代码时出现此错误。

此行有多个标记 - 语法错误,插入“枚举标识符”以完成 EnumHeader - 语法错误,插入“EnumBody”来完成EnumDeclaration

我尝试了其他答案中的所有内容,但似乎无法找出问题所在。这是我的控制器代码-

package io.springboot.topics;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TopicsController {

    @Autowired
    private TopicSrvice topicService;

    @RequestMapping("/topics")
    public List<Topic> getAllTopics() {
        return topicService.getAllTopics();
    }
    @RequestMapping("/topics/{id}")
    public Topic getTopic(@PathVariable String id) {
        return topicService.getTopic(id);
    }

 @RequestMapping(method=RequestMethod.POST,value="/topics")

}

错误出现在最后一行,即最后一个 Requestmapping()。

【问题讨论】:

  • 最后一个RequestMapping的方法在哪里?
  • 您是否尝试过使用 GetMapping 和 PostMapping 而不是 RequestMapping 来注释方法?
  • 是的。我尝试过这个。那也行不通。我不明白,如果它适用于上述两种情况,为什么它会在第三种情况下给出这个编辑器错误?我是否缺少某些语法或其他内容?
  • 请将该方法添加到您的问题中。你显然忘了添加它。
  • 您是否也遇到任何运行时或编译时错误?

标签: java spring rest spring-mvc spring-boot


【解决方案1】:

有点晚了,但是对于那些刚刚发现这个问题的人:您需要在@RequestMapping 下输入实际方法。当您在此阶段停止时,Eclipse 会出现问题,但是一旦您编写了方法,就可以开始了。至少这对我有用。

所以:

@RequestMapping(method=RequestMethod.POST,value="/topics")
public ... {
//your method
}

【讨论】:

    【解决方案2】:

    您正在为两种方法编写 /topics URL,一种用于 GET,一种用于 POST,Spring 不支持此配置,您可以为两种不同的方法更改 url,也可以编写一个具有 url /topics 和数组的方法HttpMethod 之类的方法 = { RequestMethod.GET, RequestMethod.POST }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-28
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多