JavaSpring教程示例的服务器错误500
我试着遵循JavaSpring的教程 简而言之,一个是设计一个控制器,负责4个不同的HTTP端点,这些端点是围绕视频上传服务构建的。 我的解决方案是以下控制器:JavaSpring教程示例的服务器错误500,spring,Spring,我试着遵循JavaSpring的教程 简而言之,一个是设计一个控制器,负责4个不同的HTTP端点,这些端点是围绕视频上传服务构建的。 我的解决方案是以下控制器: /* * * Copyright 2014 Jules White * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance wi
/*
*
* Copyright 2014 Jules White
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.magnum.dataup;
import org.magnum.dataup.model.Video;
import org.magnum.dataup.model.VideoStatus;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import retrofit.http.Multipart;
import retrofit.http.POST;
import retrofit.http.PUT;
import retrofit.http.Part;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
@Controller
public class Controller01 {
private Map<Long,Video> videos= new HashMap<>();
private static final AtomicLong counter= new AtomicLong(0);
private Map<Long,String> id2url= new HashMap<>();
public Video save(Video entity) {
checkAndSetId(entity);
videos.put(entity.getId(), entity);
return entity;
}
private void checkAndSetId(Video entity) {
if ( entity.getId() == 0 ) {
entity.setId(counter.incrementAndGet());
}
}
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;
}
/**
* You will need to create one or more Spring controllers to fulfill the
* requirements of the assignment. If you use this file, please rename it
* to something other than "AnEmptyController"
*
*
________ ________ ________ ________ ___ ___ ___ ________ ___ __
|\ ____\|\ __ \|\ __ \|\ ___ \ |\ \ |\ \|\ \|\ ____\|\ \|\ \
\ \ \___|\ \ \|\ \ \ \|\ \ \ \_|\ \ \ \ \ \ \ \\\ \ \ \___|\ \ \/ /|_
\ \ \ __\ \ \\\ \ \ \\\ \ \ \ \\ \ \ \ \ \ \ \\\ \ \ \ \ \ ___ \
\ \ \|\ \ \ \\\ \ \ \\\ \ \ \_\\ \ \ \ \____\ \ \\\ \ \ \____\ \ \\ \ \
\ \_______\ \_______\ \_______\ \_______\ \ \_______\ \_______\ \_______\ \__\\ \__\
\|_______|\|_______|\|_______|\|_______| \|_______|\|_______|\|_______|\|__| \|__|
*
*/
public static final String DATA_PARAMETER = "data";
public static final String ID_PARAMETER = "id";
public static final String VIDEO_SVC_PATH = "/video";
public static final String VIDEO_DATA_PATH = VIDEO_SVC_PATH + "/{id}/data";
private VideoFileManager videoDataMgr;
// You would need some Controller method to call this...
public void saveSomeVideo(Video v, MultipartFile videoData) throws IOException {
videoDataMgr.saveVideoData(v, videoData.getInputStream());
}
public void serveSomeVideo( Video v, HttpServletResponse response ) throws IOException {
// Of course, you would need to send some headers, etc. to the
// client too!
// ...
response.setContentType("video/mp4");
videoDataMgr.copyVideoData(v, response.getOutputStream());
}
@RequestMapping(value = "/video", method = RequestMethod.GET)
public @ResponseBody Collection<Video> getVideoList() {
List<Video> list= new ArrayList<>();
for ( Map.Entry<Long,Video> entry: videos.entrySet() )
list.add(entry.getValue());
return list;
}
@RequestMapping(value = "/video", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<Video>
addVideo( @RequestBody Video v ) {
Video entry= Video.create()
.withContentType("video/mp4")
.withDuration(v.getDuration())
.withSubject(v.getSubject())
.withTitle(v.getTitle()).build();
/**
* what comes
*/
entry.setLocation(v.getLocation());
/**
* what we generate
*/
String url;
checkAndSetId(entry);
entry.setDataUrl(url= getDataUrl(counter.get()));
videos.put(entry.getId(),entry);
ResponseEntity<Video> entity= new ResponseEntity<>(entry,HttpStatus.OK);
return entity;
}
@Multipart
@RequestMapping(value = VIDEO_DATA_PATH, method= {RequestMethod.PUT,RequestMethod.POST}, consumes="multipart/form-data")
@ResponseBody
public
VideoStatus
setVideoData( @PathVariable(ID_PARAMETER) long id,
@RequestParam(DATA_PARAMETER) MultipartFile videoData,
HttpServletResponse response,
HttpServletRequest request
) throws IOException {
if ( !videos.containsKey(id) ) {
response.sendError(404);
return null;
//return new ResponseEntity<VideoStatus>(new VideoStatus(VideoStatus.VideoState.READY),HttpStatus.NOT_FOUND);
}
try {
saveSomeVideo(videos.get(id),videoData);
response.setStatus(200);
return new VideoStatus(VideoStatus.VideoState.READY);
} catch (IOException e) {
response.sendError(404);
//return new ResponseEntity<VideoStatus>(new VideoStatus(VideoStatus.VideoState.READY),HttpStatus.BAD_REQUEST);
return null;
}
}
@RequestMapping(value = "/video/{id}/data", method= RequestMethod.GET)
public ResponseEntity<Video> getData(
@PathVariable("id") long id,
HttpServletResponse request
) throws IOException {
if ( !videos.containsKey(id) || !videoDataMgr.hasVideoData(videos.get(id)) ) {
ResponseEntity<Video> entity= new ResponseEntity<Video>(HttpStatus.NOT_FOUND);
return entity;
}
ResponseEntity<Video> entity= new ResponseEntity<Video>(HttpStatus.OK);
serveSomeVideo(videos.get(id),request);
request.setContentType("video/mp4");
return entity;
}
}
/*
*
*版权所有2014 Jules White
*
*根据Apache许可证2.0版(以下简称“许可证”)获得许可;
*除非遵守许可证,否则不得使用此文件。
*您可以通过以下方式获得许可证副本:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*根据许可证进行的分发是按“原样”进行分发的,
*无任何明示或暗示的保证或条件。
*请参阅许可证以了解管理权限和权限的特定语言
*许可证下的限制。
*
*/
包org.magnum.dataup;
导入org.magnum.dataup.model.Video;
导入org.magnum.dataup.model.VideoStatus;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.ResponseEntity;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.*;
导入org.springframework.web.context.request.RequestContextHolder;
导入org.springframework.web.context.request.ServletRequestAttributes;
导入org.springframework.web.multipart.MultipartFile;
导入reformation.http.Multipart;
导入reformation.http.POST;
导入reformation.http.PUT;
导入reformation.http.Part;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入java.io.IOException;
导入java.util.*;
导入java.util.concurrent.AtomicLong;
@控制器
公共类控制器01{
私有地图视频=新建HashMap();
专用静态最终AtomicLong计数器=新的AtomicLong(0);
私有映射id2url=newHashMap();
公共视频保存(视频实体){
checkAndSetId(实体);
videos.put(entity.getId(),entity);
返回实体;
}
私有void checkAndSetId(视频实体){
if(entity.getId()==0){
setId(counter.incrementAndGet());
}
}
私有字符串getDataUrl(长视频ID){
字符串url=getUrlBaseForLocalServer()+“/video/”+videoId+“/data”;
返回url;
}
私有字符串getUrlBaseForLocalServer(){
HttpServletRequest请求=
((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
线脚=
“http://”+request.getServerName()
+((request.getServerPort()!=80)?:“+request.getServerPort():”);
返回基地;
}
/**
*您需要创建一个或多个Spring控制器来实现
*分配的要求。如果使用此文件,请重命名它
*除了“AnEmptyController”之外的其他内容
*
*
________ ________ ________ ________ ___ ___ ___ ________ ___ __
|\ ____\|\ __ \|\ __ \|\ ___ \ |\ \ |\ \|\ \|\ ____\|\ \|\ \
\ \ \___|\ \ \|\ \ \ \|\ \ \ \_|\ \ \ \ \ \ \ \\\ \ \ \___|\ \ \/ /|_
\ \ \ __\ \ \\\ \ \ \\\ \ \ \ \\ \ \ \ \ \ \ \\\ \ \ \ \ \ ___ \
\ \ \|\ \ \ \\\ \ \ \\\ \ \ \_\\ \ \ \ \____\ \ \\\ \ \ \____\ \ \\ \ \
\ \_______\ \_______\ \_______\ \_______\ \ \_______\ \_______\ \_______\ \__\\ \__\
\|_______|\|_______|\|_______|\|_______| \|_______|\|_______|\|_______|\|__| \|__|
*
*/
公共静态最终字符串数据\u PARAMETER=“DATA”;
公共静态最终字符串ID\u PARAMETER=“ID”;
公共静态最终字符串VIDEO\u SVC\u PATH=“/VIDEO”;
公共静态最终字符串VIDEO_DATA_PATH=VIDEO_SVC_PATH+“/{id}/DATA”;
专用VideoFileManager videoDataMgr;
//您需要一些控制器方法来调用此。。。
public void saveSomeVideo(Video v,MultipartFile videoData)引发IOException{
videoDataMgr.saveVideoData(v,videoData.getInputStream());
}
public void serveSomeVideo(Video v,HttpServletResponse)引发IOException{
//当然,您需要将一些标题等发送到
//客户也是!
// ...
response.setContentType(“视频/mp4”);
videoDataMgr.copyVideoData(v,response.getOutputStream());
}
@RequestMapping(value=“/video”,method=RequestMethod.GET)
public@ResponseBody集合getVideoList(){
列表=新的ArrayList();
对于(Map.Entry:videos.entrySet())
list.add(entry.getValue());
退货清单;
}
@RequestMapping(value=“/video”,method=RequestMethod.POST)
public@ResponseBody ResponseEntity
addVideo(@RequestBody Video v){
视频条目=Video.create()
.withContentType(“视频/mp4”)
.withDuration(v.getDuration())
.withSubject(v.getSubject())
.withTitle(v.getTitle()).build();
/**
*会发生什么
*/
entry.setLocation(v.getLocation());
/**
*我们产生了什么
*/
字符串url;
checkAndSetId(条目);
entry.setDataUrl(url=getDataUrl(counter.get());
videos.put(entry.getId(),entry);
ResponseEntity=新的ResponseEntity(条目,HttpStatus.OK);
返回实体;
}
@多部分
@RequestMapping(value=VIDEO\u DATA\u PATH,method={RequestMethod.PUT,RequestMethod.POST},consumes=“multipart/form DATA”)
@应答器
公众的
视频状态
setVideoData(@PathVariable(ID_
videoData = VideoFileManager.get();