【发布时间】:2015-11-17 01:45:22
【问题描述】:
我正在根据在线 MOOC 中的分配创建一个 Spring Boot 应用程序。课程结束了,我只是在自己尝试作业。
这是我的 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>
这是我的控制器:
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 添加到 Class 或 addVideo 方法,那么它可以工作。
当我发送以下 Post 请求时,使用 Chrome 上的 Postmat 插件: http://localhost:8080/api/add 用这个身体做视频: { “身份证”:1, “标题”:“测试”, "contentType":"test1", "dataUrl":"testurl", "starRating":"5", “持续时间”:7
} 我在 Postman 中收到这条消息:
{ "时间戳": 1440349612613,
“状态”:405,
“错误”:“方法不允许”,
“异常”:“org.springframework.web.HttpRequestMethodNotSupportedException”,“消息”:“不支持请求方法'POST'”,“路径”:“/api/add”
}
我的 Eclipse 控制台中的这条消息: WARN PageNotFound - 不支持请求方法“POST”
但是,如果我有 @ResponseBody 到类或方法返回类型,则可以添加视频并且可以在没有任何错误的情况下检索视频。 因此,主要问题是: 如果 addVideo 没有返回任何内容,为什么我需要添加 @ResponseBody。
【问题讨论】:
-
请定义“工作”和“不工作”。你在做什么,你期望发生什么以及会发生什么?
-
我使用 postman 发送 post 请求以添加视频并收到错误消息。我正在编辑原始帖子以反映细节。谢谢。