我已經實現了一種通過張貼到Jersey JAX-RS REST後端來上傳音頻文件的方法。這工作正常。然而,當我嘗試執行DELETE音頻文件的方法,它給了我下面的錯誤:JAX-RS GET和DELETE給出405錯誤
HTTP 405 Method Not Allowed
我不能爲我的生活中看到什麼是錯的 - 我認爲這可能是一些小事,我我是盲目的,因爲我以前做過很多DELETES,而且他們都工作過。我也試圖實現一個簡單的虛擬GET方法,這也給出了相同的錯誤。
文件上傳是使用稱爲ng-file-upload的angularjs指令完成的。 它被上傳成功使用以下網址:然後
file.upload = Upload.upload({
url: 'http://localhost:8080/pododdle/webapi/auctions/' + bid.auction_id + '/uploads?category_id=' + bid.category_id,
data: {file: file}
});
用於上傳的一個例子網址是:
http://localhost:8080/pododdle/webapi/auctions/1/uploads?category_id=2
我一直在使用郵遞員使用DELETE嘗試櫃面我的前端代碼是錯誤的。提供的網址是:
http://localhost:8080/pododdle/webapi/auctions/1/uploads
以下是整個「上傳」資源的代碼,包括DELETE和POST。正如你所看到的,DELETE只是一種虛擬方法,沒有真正的工作。另外,上傳資源是「拍賣」資源的子資源。
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UploadResource {
private static final Logger log = Logger.getLogger(UploadResource.class);
AuctionService auction_service = new AuctionService();
@DELETE
public Response deleteAudio(@PathParam("auction_id") int auction_id) {
System.out.println("inside deleteAudio");
return Response.ok().build();
}
//this uploads the audio file, stores it to disk, and then saves the file-path to the auction
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("text/html")
public Response uploadAudio(@PathParam("auction_id") int auction_id, @QueryParam("category_id") int category_id,
@FormDataParam("file") InputStream fileInputString, @FormDataParam("file") FormDataContentDisposition fileInputDetails) {
//We need to get the value of the audio root directory
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Properties properties = new Properties();
try {
properties.load(classLoader.getResourceAsStream("pododdle.properties"));
} catch (IOException e) {
log.error("Error getting the audio root directory location when uploading the audio: " + e.getMessage());
e.printStackTrace();
}
String auctionAudio = "audio/category/" + category_id + "/auction/" + auction_id + "/" + fileInputDetails.getFileName();
String audioFileName = properties.getProperty("audioRootDirectory") + auctionAudio;
NumberFormat myFormat = NumberFormat.getInstance();
myFormat.setGroupingUsed(true);
try {
File audioFile = new File(audioFileName);
File parent = audioFile.getParentFile();
if(!parent.exists() && !parent.mkdirs()) {
return Response.status(501).entity("Error creating directory when uploading the audio to the server. Please try again in a few moments").build();
}
OutputStream out = new FileOutputStream(audioFile, false);
byte[] buffer = new byte[1024];
int bytes = 0;
long file_size = 0;
while((bytes = fileInputString.read(buffer)) != -1) {
out.write(buffer, 0, bytes);
file_size += bytes;
}
out.flush();
out.close();
log.info(String.format("Uploading Audio file for category: " + category_id + ", fileSize: %s", myFormat.format(file_size)));
} catch (IOException ioe) {
System.out.println("error uploading audio file to server: " + ioe.toString());
return Response.status(501).entity("Error uploading the audio to the server. Please try again in a few moments").build();
}
//now updating the auction with the audio path in the database and return the updated auction
return Response.ok(auction_service.updateAuction(auction_id, auctionAudio).toString()).build();
}
}
拍賣資源是這樣的:
@Path("/auctions")
public class AuctionResource {
//creating sub-resource for auction bids
@Path("/{auction_id}/uploads")
public UploadResource getUploadResource()
{
return new UploadResource();
}
}
它與你的問題沒有關係,但是使用'String.format'來最終在你的格式中插入'category_id' ... –