2017-02-02 13 views
0

私はここに春GETとPATCHリクエスト

はここに私のREST CONTROLLERコード

@RestController 
public class MainRestController { 

@RequestMapping(value = "/",method=RequestMethod.POST) 
public String startMigration(){ 
     return "POSt"; 
} 

@RequestMapping(value = "/",method=RequestMethod.PATCH) 
public String PUT(){ 
     return "PUT"; 
} 

/* 
* If i commnet this method and un comment following method it will run 
* */ 
@RequestMapping(value = "/{name}",method=RequestMethod.PUT) 
public String PATCH(@PathVariable String name){ 
     return "PATCH"; 
} 

/*@RequestMapping(value = "/",method=RequestMethod.PATCH) 
public String PATCH(){ 
     return "PATCH"; 
}*/ 

@RequestMapping(value = "/",method=RequestMethod.DELETE) 
public String DELETE(){ 
     return "DELETE"; 
} 
} 

で、私の春ブートRESTコントローラで2メソッドを持っていることは、私のコントローラのコード

@Controller 
public class MainController { 

@RequestMapping(value = "/",method=RequestMethod.GET) 
public String getMainPage(){ 
    return "index.html"; 
} 
} 

さて問題です私がPATCH要求を打つときです http://localhost:8080/ 返す

{ 
"timestamp": 1486041782895, 
"status": 405, 
"error": "Method Not Allowed", 
"exception":"org.springframework.web.HttpRequestMethodNotSupportedException", 
"message": "Request method 'PATCH' not supported", 
"path": "/" 
} 

と私はGETリクエストhttp://localhost:8080/を打ったとき、それは

Whitelabel Error Page 
This application has no explicit mapping for /error, so you are seeing this as a fallback. 
Thu Feb 02 19:00:18 IST 2017 
There was an unexpected error (type=Method Not Allowed, status=405). 
Request method 'GET' not supported 

は、誰が私にその理由を伝えることができ返しますか?

+0

SBアプリケーションを紹介するサンプル要点を追加できますか? – dmahapatro

+0

問題のクラスコードを両方追加しました – atiwari54

答えて

0

今の問題は、私はPATCH要求http://localhost:8080/を打ったとき、それはあなたがこの方法でPathVariableを期待する方法を宣言しているため、このエラーが発生し

{ 
"timestamp": 1486041782895, 
"status": 405, 
"error": "Method Not Allowed", 
"exception":"org.springframework.web.HttpRequestMethodNotSupportedException", 
"message": "Request method 'PATCH' not supported", 
"path": "/" 
} 

返しです。あなたはそれが変数を渡さずに仕事をしたい場合は、あなたが何かやるべき

:だからこのPATCH要求http://localhost:8080/

を「別のパッチ」を返す必要があります

@RequestMapping(method=RequestMethod.PATCH) 
public @ResponseBody String patch(@RequestParam(name = "name", required = false) String name){ 
    return "ANOTHER PATCH"; 
} 

これはあなたの質問ましたか?

+0

私の問題点をGETリクエストで解決しました。私はPATCHメソッドをコメントしています。 – atiwari54

+0

私は専門家ではありませんが、RestControllerではなくControllerでクラスを作成しました。 RestControllerアノテーションでは、ResponseBodyメソッドがカプセル化されています。 Controllerを使用する場合は、外部クライアントの機能とクラスパスで使用可能なライブラリに従って戻り値を自動的にシリアライズするResponseBodyアノテーション(クラスまたはメソッド内)を使用する必要があります。 おそらく、何人かの専門家が私たちに理由を説明することができます。 –

+0

はい私はUI(html)のためのGETとPATCHメソッドを含むWebサービスのための1つのRESTコントローラを書いた1つのコントローラを持っています。 私がrequestmappingでエイリアスを与えると正しく動作しますが、 "/"だけで動作するエイリアスを与えていないと動作しません – atiwari54

関連する問題