如何处理错误的 PathVariable

huangapple go评论111阅读模式
英文:

How to handle wrong PathVariable

问题

我有一个端点:

  1. @GetMapping("/student/{id}")
  2. public Student getStudentByID(@PathVariable Long id)

如何处理这种情况:/student/aaa
当用户传递的不是Long类型的值时。现在我收到了400错误请求和空JSON作为上述请求的响应。

英文:

I have endpoint :

  1. @GetMapping("/student/{id})
  2. public Student getStudentByID(@PathVariable Long id)

How to handle the situation: /student/aaa
When the user passes something else than Long. Now I have received 400 Bad Request and Empty JSON for the above request.

答案1

得分: 1

为了控制错误处理,您可以接受一个字符串参数,并在转换为长整型失败时捕获异常。

  1. public Student getStudentByID(@PathVariable String idString) {
  2. try {
  3. Long id = Long.valueOf(idString);
  4. } catch (NumberFormatException e) {
  5. // 处理非长整型值
  6. }
  7. }
英文:

To control your error handling, you can accept String parameter and catch Exception if failed to convert to long

  1. public Student getStudentByID(@PathVariable String idString) {
  2. try {
  3. Long id = Long.valueOf(idString);
  4. } catch(NumberFormatException e) {
  5. // handle non long value
  6. }

答案2

得分: 1

400状态码是根据您的编码而来的良好响应。

请参考以下示例:

以下API将接受整数。示例:student/123

  1. @GetMapping("/student/{id}")
  2. public Student getStudentByID(@PathVariable Long id)

以下API将接受字符串。示例:/student/aaa

  1. @GetMapping("/student/{id}")
  2. public Student getStudentByID(@PathVariable String id)

如果您想要添加验证,请使用以下代码:

  1. @GetMapping("/student/{id}")
  2. public ResponseEntity<Student> getStudentByID(@PathVariable String id) {
  3. try {
  4. Long id = Long.valueOf(id);
  5. } catch(NumberFormatException e) {
  6. return ResponseEntity.notFound().build();
  7. }
  8. return ResponseEntity.ok(new Student());
  9. }

小建议,始终在您的服务中使用ResponseEntity。这样您就可以控制/返回状态码。参考:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html

英文:

400 Status code is a good response as per your coding.

See the below examples:

Below api will accept the integer. example student/123.

  1. @GetMapping(&quot;/student/{id}&quot;)
  2. public Student getStudentByID(@PathVariable Long id)

Below api will accept the string. example /student/aaa

  1. @GetMapping(&quot;/student/{id}&quot;)
  2. public Student getStudentByID(@PathVariable String id)

If you want add validation use the below code:

  1. @GetMapping(&quot;/student/{id}&quot;)
  2. public ResponseEntity&lt;Student&gt; getStudentByID(@PathVariable String id) {
  3. try {
  4. Long id = Long.valueOf(id);
  5. } catch(NumberFormatException e) {
  6. return ResponseEntity.notFound().build();
  7. }
  8. return ResponseEntity.ok(new Student());
  9. }

Small Advice, always use the, ResponseEntity in your services. So you can control/return the status code. Reference: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html

huangapple
  • 本文由 发表于 2020年7月23日 18:43:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63052413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定