方法'POST'不支持405

时间:2017-06-15 07:54:59

标签: java spring spring-mvc post

我有一个POST方法用于上传文件。要上传文件,请在Token

上传递header
private RessourceMetadata doSave(Ressource ressource) throws IOException, FileNotFoundException {
        String tempFilePath = writeDocumentToTempFile(ressource);
        MultiValueMap<String, Object> parts = createMultipartFileParam(tempFilePath);
        RessourceMetadata ressourceMetadata = getRestTemplate().postForObject(
                getServiceUrl()
                        + "/uploadFileProperties?group={group}&projectId={projectId}&version={version}&env={env}",
                parts, RessourceMetadata.class, ressource.getGroup(), ressource.getId(), ressource.getVersion(),
                ressource.getEnv());
        return ressourceMetadata;
    }

...

private MultiValueMap<String, Object> createMultipartFileParam(String tempFilePath) {
    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
    parts.add("file", new FileSystemResource(tempFilePath));
    parts.add("Authorization", "hrYIwyXoKGkY2juJfJb012Qx768RtUYA");
    return parts;
}

错误:

o.s.w.s.PageNotFound - Request method 'POST' not supported
 2017-Jun-15 09:50:31.852 ERROR [main] f.g.d.d.w.c.ServiceClient - Error while uploading file
 org.springframework.web.client.HttpClientErrorException: 405 Method Not Allowed

我的代码出了什么问题?

POST方法:

@RequestMapping(value = "/uploadFileProperties", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header") })
public @ResponseBody RessourceMetadata uploadProperties(
        @ApiParam(name = "file", value = "fichier properties à uploader", defaultValue = "", required = true) @RequestParam("file") MultipartFile file,
        @ApiParam(name = "group", value = "groupe projet", defaultValue = "", required = true) @RequestParam("group") String group,
        @ApiParam(name = "projectId", value = "id projet (francevisas, diplomatie...)", defaultValue = "", required = true) @RequestParam("projectId") String projectId,
        @ApiParam(name = "version", value = "version du projet", defaultValue = "", required = true) @RequestParam("version") String version,
        @ApiParam(name = "env", value = "environnement (dev, prod, formation...)", defaultValue = "", required = true) @RequestParam("env") String env) {

    try {
        Ressource ressource = new Ressource(file.getBytes(), group, projectId, version, env,
                PropertiesFileUtils.getPropertiesFilename());
        getRessourceService().save(ressource);
        return ressource.getMetadata();
    } catch (RuntimeException e) {
        log.error("Error while uploading.", e);
        throw e;
    } catch (Exception e) {
        log.error("Error while uploading.", e);
        throw new RuntimeException(e);
    }
}

1 个答案:

答案 0 :(得分:0)

在doSave()方法中,调用getServiceUrl()方法并添加路径。 getServiceUrl()必须返回其他一些域。要检查它,请进入doSave()方法的调试模式,看看getService方法是否为您提供了正确的值。另外,尝试使用getServiceUrl从Postman为您提供的域的POST请求。

相关问题