405 Spring MVC中不允许使用的方法

时间:2015-11-30 16:18:34

标签: spring spring-mvc file-upload

我正在使用Spring版本4.2.3,Spring启动版本1.3.0。

当我使用Advanced rest client将文件上传到我的服务时,我收到405 Method Not Allowed错误。

我的控制器是:

@RequestMapping(value = "/uploadDocXFileMul", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public
@ResponseBody
 HttpEntity<String> upload(@RequestParam("fileUpload") MultipartFile file, @RequestBody FileDTO fileDTO){
    /**
    work with file

    **/
}

我的FileDTO是

public class FileDTO implements Serializable {

private static final long serialVersionUID = -211383758881523704L;

@NotEmpty
private String type;
@NotNull
private Date createdDate;
@NotEmpty
private String clientId;

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public Date getCreatedDate() {
    return createdDate;
}

public void setCreatedDate(Date createdDate) {
    this.createdDate = createdDate;
}

public String getClientId() {
    return clientId;
}

public void setClientId(String clientId) {
    this.clientId = clientId;
}

My Rest客户端图片是 enter image description here

enter image description here

我的错误讯息是:

Status
405 Method Not Allowed Show explanation Loading time: 792
Request headers 
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryFGAn4APIXqrAvDlW 
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Response headers 
Server: Apache-Coyote/1.1 
Allow: HEAD, GET 
Content-Type: text/html;charset=utf-8 
Content-Language: en 
Content-Length: 1090 
Date: Mon, 30 Nov 2015 16:00:06 GMT 
  

注意:我对泉水很新,请帮助我

1 个答案:

答案 0 :(得分:0)

我认为你的问题是因为

@RequestBody FileDTO fileDTO

这会将您的请求正文映射到该对象。但你也告诉String你只有cosume

consumes = MediaType.MULTIPART_FORM_DATA_VALUE

您发送的请求就像表单数据一样,并且您的控制器中没有表单映射。

我建议你用两个方法分隔它,一个用fileupload,另一个用你的RequestBody,你可以发送json数据而不是表单数据。