2017-03-02 121 views
0

我正在試用Spring Boot,我正面臨着一個小問題。我無法在@RestController中使用@PathVariable。這裏是我的控制器 -@PathVariable不能與@RestController一起工作

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); 
    } 
} 

這是我打電話是終點 - http://localhost:8080/user/1/

在我以前的春天(不啓動)的項目,我用@PathVariable在@Controller和它工作得很好。我無法弄清楚我可能做錯了什麼。想法?

+1

什麼是「不工作」的意思?像例外,http響應等... – Blank

回答

1

元件namevalue具有不同的含義。
見文件:
RequestMapping

更換namevalue在RequestMapping註釋
@RequestMapping(value = "/user/{id}/", method = RequestMethod.GET)

OR

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

他們只是一點點shorte河

0

試試下面的代碼 -

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

如果你有一個服務層爲寫返回serviceLayerName.generateUser(ID);