Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 注释RequestMapping()给出了错误_Java_Spring_Rest_Spring Mvc_Spring Boot - Fatal编程技术网

Java 注释RequestMapping()给出了错误

Java 注释RequestMapping()给出了错误,java,spring,rest,spring-mvc,spring-boot,Java,Spring,Rest,Spring Mvc,Spring Boot,我从SpringBoot开始,并尝试做一个Rest服务。 我正在编写一个控制器,其中有到3个方法的requestmapping。 其中两个工作正常,而thirl注释在编写代码时给出此错误 这条线上有多个标记 -语法错误,请插入“枚举标识符”以完成枚举标头 -语法错误,请插入“EnumBody”以完成EnumDeclaration 我尝试了其他答案,但似乎无法找出问题所在。这是我的控制器代码- package io.springboot.topics; import java.util.Arra

我从SpringBoot开始,并尝试做一个Rest服务。 我正在编写一个控制器,其中有到3个方法的requestmapping。 其中两个工作正常,而thirl注释在编写代码时给出此错误

这条线上有多个标记 -语法错误,请插入“枚举标识符”以完成枚举标头 -语法错误,请插入“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")
包io.springboot.topics;
导入java.util.array;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RestController;
@RestController
公共类TopicsController{
@自动连线
私人topicService topicService;
@请求映射(“/topics”)
公共列表getAllTopics(){
返回topicService.getAllTopics();
}
@请求映射(“/topics/{id}”)
公共主题getTopic(@PathVariable字符串id){
返回topicService.getTopic(id);
}
@RequestMapping(method=RequestMethod.POST,value=“/topics”)
}


错误出现在最后一行ie last Requestmapping()。

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

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

因此:


上一个
RequestMapping
的方法在哪里?你有没有尝试过用GetMapping和PostMapping而不是RequestMapping来注释方法?是的。我试过了。这也不起作用。我不明白,如果它对上述两种情况都起作用,为什么第三种情况会出现编辑器错误?我是否缺少一些语法或什么?请将该方法添加到您的问题中。您显然忘记添加该方法。您是否也收到任何运行时或编译时错误?
@RequestMapping(method=RequestMethod.POST,value="/topics")
public ... {
//your method
}