Java 分级机测试出错。无限递归误差
我正在网上学习SpringMVC。作为我课程的一部分,我必须开发一个云视频服务。这里提到了规格。 下面是我的控制器类Java 分级机测试出错。无限递归误差,java,spring,spring-mvc,retrofit,Java,Spring,Spring Mvc,Retrofit,我正在网上学习SpringMVC。作为我课程的一部分,我必须开发一个云视频服务。这里提到了规格。 下面是我的控制器类 @Controller 公共级视频控制器{ 专用最终AtomicLong currentId=新的AtomicLong(1L) //保存传入视频元数据的映射 私有HashMap videoMap=新HashMap(); //接收到/视频的GET请求并返回当前列表 //内存中的视频列表 @RequestMapping(value=“/video”,method=RequestM
@Controller
公共级视频控制器{
专用最终AtomicLong currentId=新的AtomicLong(1L)
//保存传入视频元数据的映射
私有HashMap videoMap=新HashMap();
//接收到/视频的GET请求并返回当前列表
//内存中的视频列表
@RequestMapping(value=“/video”,method=RequestMethod.GET)
public@ResponseBody List getVideoList()引发IOException{
List resultList=new ArrayList();
for(长id:videoMap.keySet()){
add(videoMap.get(id));
}
返回结果列表;
}
//接收到/视频的POST请求并添加视频对象
//从请求数据创建到映射
@RequestMapping(value=“/video”,method=RequestMethod.POST)
public@ResponseBody()Video addVideoMetadata(@RequestBody Video data)
{
//创建视频对象
视频=Video.create().withContentType(data.getContentType())
.withDuration(data.getDuration())
.withSubject(data.getSubject())
.withTitle(data.getTitle()).build();
//设置视频的id
long videoId=currentId.incrementAndGet();
video.setId(videoId);
//设置此视频的URL
字符串videoURL=getDataUrl(videoId);
video.setDataUrl(videoURL);
//保存要映射的视频元数据对象
视频v=保存(视频);
返回v;
}
//接收对/video/{id}/data的POST请求,例如/videoa/2/data
//上载作为多部分文件发送的视频文件
//并将其写入光盘
@RequestMapping(value=“/video/{id}/data”,method=RequestMethod.POST)
public@ResponseBody ResponseEntity上传视频
(@RequestParam(“data”)多部分文件数据,
@PathVariable(“id”)长id,
HttpServletResponse
)抛出IOException
{
//如果id为的视频不存在
如果(!videoMap.containsKey(id)){
System.out.println(“此id不存在”);
返回新的ResponseEntity(null,HttpStatus.NOT_FOUND);
//返回新的ResponseEntity(null,HttpStatus.NOT_FOUND);
}
InputStream in=null;
试一试{
//读取输入流
in=data.getInputStream();
}
捕获(IOIE){
System.out.println(“异常读取inputstream”);
}
最后{
in.close();
}
//获取视频
Video v=videoMap.get(id);
//将其写入磁盘
VideoFileManager.get().saveVideoData(v,in);
VideoStatus vs=新的VideoStatus(VideoStatus.VideoStatus.READY);
返回新的响应状态(vs,HttpStatus.OK);
//答复:setStatus(200);
//返回新的响应状态(vs,HttpStatus.OK);
}
//读取对/vide/{id}/数据的GET请求并返回视频
//二进制数据作为输出流
@RequestMapping(value=“/video/{id}/data”,method=RequestMethod.GET)
public@ResponseBody ResponseEntity getBinaryData(
@PathVariable(“id”)长视频id,
HttpServletResponse(响应)引发IOException{
//如果id不正确
如果(!videoMap.containsKey(videoId)){
返回新的ResponseEntity(null,HttpStatus.NOT_FOUND);
}
//从地图上获取视频
Video outVideo=videoMap.get(videoId);
VideoFileManager vm=VideoFileManager.get();
//将二进制数据写入OutputStream
OutputStream os=response.getOutputStream();
复制视频数据(outVideo,os);
返回新的响应状态(os,HttpStatus.OK);
}
//将传入的视频元数据保存到地图
公共视频保存(视频v){
checkAndSetId(v);
如果(!videoMap.containsKey(v.getId())){
videoMap.put(v.getId(),v);
}
返回v;
}
//用于生成视频url的帮助器方法
私有字符串getDataUrl(长视频ID){
字符串url=getUrlBaseForLocalServer()+“/video/”+videoId+“/data”;
返回url;
}
私有字符串getUrlBaseForLocalServer(){
HttpServletRequest请求=
((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
字符串基=
“http://”+request.getServerName()
+((request.getServerPort()!=80)?:“+request.getServerPort():”);
返回基地;
}
私有void checkAndSetId(视频实体){
if(entity.getId()==0){
entity.setId(currentId.incrementAndGet());
}
}
}
现在,我通过了AutoGradingTest.java单元代码中的所有测试,但没有通过testAddVideoData()。它给出一个套接字超时错误,后跟无限递归错误,指向AutoGradingTest.java中的第159行
从逻辑上讲,我的代码似乎很好。很多其他的学习者也面临着口渴的问题,但课程导师并没有提供帮助。
有人能帮我吗?非常感谢。第159行是什么??请发布堆栈跟踪。@papaya这是行''''byte[]retrievedFile=IOUtils.toByteArray(videoData);''在AutoGradingTest.java中,您希望我们找到
AutoGradingTest.class
?它在哪里?从堆栈跟踪添加前几行,因为它正在运行到数百行org.springframework.web.util.NestedServletException:请求处理失败;嵌套的异常是org.springframework.http.converter.HttpMessageNotWritableException:无法写入JSON:无限递归(StackOverflowerr)(通过引用链:org.eclipse.jetty.server.HttpChannelState[“httpChannel”]->org.eclipse.jetty.server.HttpConnection$HttpChannelOverHttp[“state”]->org.eclipse.jetty.server.HttpChannelState[“httpChannel”]->org.eclipse.jetty.server.HttpConnection$HttpChannelOveDear@papaya,很抱歉给您添麻烦。代码库位于我在第一篇文章中共享的链接处。实际上,类之间存在依赖关系,并且有很多,因此我放置了该链接。无法将所有代码放置在文章中。谢谢
//A Map to hold incoming Video meta data
private HashMap<Long, Video> videoMap = new HashMap<Long, Video>();
//Receives GET requests to /video and returns the current list
// list of videos in memory
@RequestMapping(value = "/video", method = RequestMethod.GET)
public @ResponseBody List<Video> getVideoList() throws IOException{
List<Video> resultList = new ArrayList<Video>();
for(Long id : videoMap.keySet()) {
resultList.add(videoMap.get(id));
}
return resultList;
}
//Receives POST requests to /video and adds the video object
//created from request data to the Map
@RequestMapping(value = "/video", method = RequestMethod.POST)
public @ResponseBody() Video addVideoMetadata(@RequestBody Video data)
{
//create a Video object
Video video = Video.create().withContentType(data.getContentType())
.withDuration(data.getDuration())
.withSubject(data.getSubject())
.withTitle(data.getTitle()).build();
//set the id for the video
long videoId = currentId.incrementAndGet();
video.setId(videoId);
//set the URL for this Video
String videoURL = getDataUrl(videoId);
video.setDataUrl(videoURL);
//save the Video metadata object to map
Video v = save(video);
return v;
}
// Receives POST requests to /video/{id}/data e.g. /videoa/2/data
// uploads the video file sent as MultipartFile
// and writes it to the disc
@RequestMapping(value = "/video/{id}/data", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<VideoStatus> uploadVideo
(@RequestParam("data") MultipartFile data,
@PathVariable("id") long id,
HttpServletResponse response
) throws IOException
{
// if video with id not present
if(!videoMap.containsKey(id)) {
System.out.println(" this id not present");
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
//return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
}
InputStream in = null;
try {
//read the input stream
in = data.getInputStream();
}
catch(IOException ie){
System.out.println("Exception reading inputstream");
}
finally {
in.close();
}
//get the video
Video v = videoMap.get(id);
//write it to disk
VideoFileManager.get().saveVideoData(v, in);
VideoStatus vs = new VideoStatus(VideoStatus.VideoState.READY);
return new ResponseEntity<>(vs, HttpStatus.OK);
//response.setStatus(200);
//return new ResponseEntity<>(vs, HttpStatus.OK);
}
//Reads GET request to /vide/{id}/data and returns the video
//binary data as output stream
@RequestMapping(value = "/video/{id}/data", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<OutputStream> getBinaryData(
@PathVariable("id") long videoId,
HttpServletResponse response) throws IOException {
//if id is incorrect
if(!videoMap.containsKey(videoId)) {
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
}
//get the video from Map
Video outVideo = videoMap.get(videoId);
VideoFileManager vm = VideoFileManager.get();
//write the binary data to OutputStream
OutputStream os = response.getOutputStream();
vm.copyVideoData(outVideo, os);
return new ResponseEntity<>(os, HttpStatus.OK);
}
//save incoming video metadata to a Map
public Video save(Video v) {
checkAndSetId(v);
if(!videoMap.containsKey(v.getId())) {
videoMap.put(v.getId(), v);
}
return v;
}
//helper method to generate url for video
private String getDataUrl(long videoId){
String url = getUrlBaseForLocalServer() + "/video/" + videoId + "/data";
return url;
}
private String getUrlBaseForLocalServer() {
HttpServletRequest request =
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String base =
"http://"+request.getServerName()
+ ((request.getServerPort() != 80) ? ":"+request.getServerPort() : "");
return base;
}
private void checkAndSetId(Video entity) {
if(entity.getId() == 0){
entity.setId(currentId.incrementAndGet());
}
}