Gin でデフォルト ルーターといくつかのルートをセットアップしました。
router := gin.Default()
router.POST("/users", save)
router.GET("/users",getAll)
404 Route Not Found in Gin を処理するにはどうすればよいですか?
もともと、私はジンが使用していることを理解しているhttprouterを使用していたので、これは私が元々持っていたものでした...
router.NotFound = http.HandlerFunc(customNotFound)
そして機能:
func customNotFound(w http.ResponseWriter, r *http.Request) {
//return JSON
return
}
しかし、これはジンでは機能しません。
使用できるように、を使用して JSON を返すことができる必要がありc *gin.Context
ます。
c.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message": "Page not found"})