ジョンコード:
私は、春ブーツで、この極めてシンプルなAPIを持っています
@RestController
@RequestMapping(value = "/users")
public class UserController {
@Autowired
private IUserRepository userRepository;
@PostMapping()
public UserModel createUser(@RequestBody UserModel user){
return userRepository.save(user);
}
@GetMapping()
public List<UserModel> getUsers(){
return userRepository.findAll();
}
@GetMapping(value = "{id}")
public Optional<UserModel> getUserById(@RequestParam("id") String id) {
return userRepository.findById(id);
}
@GetMapping(value="/searchByName/{name}")
public List<UserModel> requestMethodName(@RequestParam("name") String name) {
return userRepository.findByName(name);
}
}
私は実行すると、それがうまくコンパイルし、エラーをスローしませんが、私はGETリクエストを作成するとき、次のようにその要求をミスマッチ:
私がやるとき:「のhttp:// localhostを:2202 /ユーザーは」私は問題なく、すべてのユーザーのリストを取得し、
しかし、私が呼ぶとき:「のhttp:// localhostを:?2202 /ユーザID = 5e6f5b4d19d83c38af6c648d」私はまだすべてのユーザーのリストを取得します。私は、コントローラの「getUserById」アクションにブレークポイントを配置すると、ブレークポイントがヒットしたことがありません。
また、私が呼ぶとき: " のhttp:// localhostを:?2202 /ユーザー/ searchByName名= Runtebala "私は400不正な要求を取得します。
これは狂気私を運転している、誰もが解決策を持っているのでしょうか?お願いします
Rayen Rejeb:
更新@GetMapping値getUserById
このように:
@GetMapping(value = "/{id}") // add '/' before the parameter
public Optional<UserModel> getUserById(@PathVariable("id") String id) {
return userRepository.findById(id);
}
以下のためにrequestMethodName
あなたは要求を修正する必要があります。名前の前に「/」追加します。
"http://localhost:2202/users/searchByName/Runtebala"
そして、このようなあなたの方法を更新します。
@GetMapping(value="/searchByName/{name}")
public List<UserModel> requestMethodName(@PathVariable("name") String name) {
return userRepository.findByName(name);
}
あなたが@RequestParamを使用する必要がある場合のマッピング値にパラメータを指定し、ちょうど@RequestParamであなたのメソッドのparamsを放置しないでください。
それは、幸運をお役に立てば幸い!