2017-03-02 3 views
0

私はSpring Bootを試していますが、私は些細な問題に直面しています。 @RestControllerで@PathVariableを使用できません。 http://localhost:8080/user/1/@PathVariableが@RestControllerと連携していません

私の前の春に(起動しない)プロジェクトは、私が@Controllerで@PathVariableを使用し、それがうまく働いた -

package com.harshil.controller; 

import org.springframework.web.bind.annotation.*; 

@RestController 
public class UserController { 

    @RequestMapping(name = "/user/{id}/", method = RequestMethod.GET) 
    public User getUser(@PathVariable("id") int id) { 
     return generateUser(id); 
    } 
} 

これは私がエンドポイントを呼んでいる方法です - ここに私のコントローラです。私は何が間違っているのか分かりません。そしてアイデア?

+1

正確に「機能していない」とは何ですか?例外のように、http応答など... – Blank

答えて

1

要素nameおよびvalueは、異なる意味を有する。
参照ドキュメント:
RequestMapping

彼らはほんの少しshorteあるごRequestMapping注釈でvalue
@RequestMapping(value = "/user/{id}/", method = RequestMethod.GET)

OR

@GetMapping("/user/{id}/")
@GetMapping(value = "/user/{id}/")

nameを交換してくださいr。

0

は、以下のコード -

@RequestMapping(name = "/user/{id}") 
     public @ResponseBody User getUser(@PathVariable(value ="id") int id) { 
      return generateUser(id); 
     } 
    } 

を試してみて、あなたはサービス層を持っている場合serviceLayerName.generateUser(ID)を返し書き込み - 。

関連する問題