请求方法' PUT'不支持StatusCode:405

时间:2017-05-24 22:02:44

标签: java c# rest put

我收到此警告

2017-05-25 00:48:43.125  WARN 7104 --- [nio-8080-exec-4] o.s.web.servlet.PageNotFound             : Request method 'PUT' not supported 
当我尝试从c#(Visual Studio)进行更新时,在smartJ中

。但是,当我从Java进行更新时,它可以工作。

Java中的更新方法:

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public Proba update(@RequestBody Proba proba) {
    System.out.println("Updating proba ...");
    try{
        probaRepository.update(proba.getIdProba(),proba);
    } catch (Exception e){
        e.printStackTrace();
    }
    return proba;
}

C#方法

static async Task<string> UpdateProbaAsync(string path, Proba proba)
    {
        string res = null;
        HttpResponseMessage response = await client.PutAsJsonAsync(path, proba);
        if (response.IsSuccessStatusCode)
        {
            res = await response.Content.ReadAsStringAsync();
        }
        return res;
    }

以下是我如何调用UpdateProbaAsync

        var rezU = await UpdateProbaAsync("http://localhost:8080/concurs/probe", new Proba(9, "cautare comori UPDATE", "3-5 ani", 0));
        var probeUpdate = await GetProbaAsync("http://localhost:8080/concurs/probe");
        foreach (var proba in probeUpdate)
        {
            Console.WriteLine(proba.ToString());
        }

这是我获得的HttpResponseMessage响应

 +      response    {StatusCode: 405, ReasonPhrase: '', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Transfer-Encoding: chunked
  Connection: close
  Date: Wed, 24 May 2017 21:57:39 GMT
  Allow: POST
  Allow: GET
  Content-Type: application/json; charset=UTF-8
}}  System.Net.Http.HttpResponseMessage

1 个答案:

答案 0 :(得分:0)

我猜你错过了c#

中的属性
    [HttpPut]   //or [HttpPost]
    [Route("/{path}")]
 async Task<string> UpdateProbaAsync(string path, Proba proba)
  {
  }

默认为GET,您正试图加入GET API