我正在根据在线 MOOC 中的作业创建一个 spring boot 应用程序。类(class)结束了,我正在自己尝试作业。
这是我的 pom.xml 文件:
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples.service.service</groupId>
<artifactId>VideoManagerServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<!-- Generic properties -->
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Web -->
<jsp.version>2.2</jsp.version>
<jstl.version>1.2</jstl.version>
<servlet.version>2.5</servlet.version>
<!-- Spring -->
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>
<!-- Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Spring MVC -->
<!-- Other Web dependencies -->
<!-- Spring and Transactions -->
<!-- Logging with SLF4J & LogBack -->
<!-- Hibernate -->
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
</dependencies>
这是我的 Controller :
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.hakeem.videoserver.model.Video;
import com.hakeem.videoserver.service.VideoService;
@RestController
@RequestMapping(value="/api",consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
public class VideoController {
@Autowired
VideoService videoService;
@RequestMapping(value="/add",method=RequestMethod.POST)
public void addVideo(@RequestBody Video video){
videoService.addVideo(video);
}
@RequestMapping(value="/all",method=RequestMethod.GET)
public @ResponseBody ArrayList<Video> getAllVideos(HttpServletResponse response){
ArrayList<Video> videos;
videos = videoService.getAllVideos();
if(videos.size() == 0){
response.setStatus(HttpStatus.NO_CONTENT.value());
}
return videos;
}
@RequestMapping(value="/delete",method=RequestMethod.DELETE)
public void deleteVideo(@RequestBody Video video){
videoService.deleteVideo(video);
}
@RequestMapping(value="/find/{id}")
public @ResponseBody Video findVideo(@PathVariable int id, HttpServletResponse response){
return videoService.findVideo(id);
}
@RequestMapping(value="/testing",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody int testEndPoint(HttpServletResponse response){
return 10;
}
}
问题是我的 addVideo 端点仅在我使用 @RestController 注释时有效,但在我使用 @Controller 注释时无效。
但是,当我使用@RestController 和@Controller 进行注释时,我的testEndpoint 方法有效
但是,如果我将 @ResponseBody 添加到类或 addVideo 方法中,它就会起作用。
当我发送以下 Post 请求时,使用 Chrome 上的 Postmat 插件: http://localhost:8080/api/add 用这个 body 做视频: { “编号”:1, “标题”:“测试”, “内容类型”:“测试1”, “dataUrl”:“testurl”, "星级":"5", “持续时间”:7
} 我在 Postman 中收到此消息:
{ "时间戳": 1440349612613,
“状态”:405,
“错误”:“不允许的方法”,
"exception":"org.springframework.web.HttpRequestMethodNotSupportedException", "message": "不支持请求方法'POST'", "path": "/api/add"
我的 eclipse 控制台中显示了这条消息: 警告 PageNotFound - 不支持请求方法“POST”
但是如果我对类或方法返回类型使用@ResponseBody,则视频已添加并且可以毫无错误地检索。 因此,主要问题是: 如果 addVideo 没有返回任何内容,为什么我需要添加 @ResponseBody。
最佳答案
这里有两个问题:
- 为什么@RestController 有效,而@Controller 无效?
如果您检查 documentation , 你会看到 RestController 是
A convenience annotation that is itself annotated with @Controller and @ResponseBody.
- 如果返回类型为void,为什么还需要添加@ResponseBody?
这样做的原因是即使没有你想要返回的实际值,也会有一个响应发送给客户端(至少是一个 HTTP OK)。因此,您需要使用其中之一:
@ResponseBody
@ResponseStatus(value = HttpStatus.OK)
关于java - spring boot注解混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32168689/