-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
実装例にオリジナルの仕様を追加 #1
base: main
Are you sure you want to change the base?
Conversation
public class DemoController { | ||
|
||
@GetMapping("/names") | ||
public List<String> getName(){ | ||
return List.of("Ichiro","Jiro", "Saburo"); | ||
public String getName( @RequestParam("id") @Pattern (regexp = "^[1-3]$" ) String id) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public List<String> getName(){ | ||
return List.of("Ichiro","Jiro", "Saburo"); | ||
public String getName( @RequestParam("id") @Pattern (regexp = "^[1-3]$" ) String id) { | ||
Map<Integer, String> namesById = Map.of(1,"Ichiro",2, "Jiro",3,"Saburo"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
@GetMapping("/fruit") | ||
public List<String> getFruitList(@RequestParam("priceMin") @Pattern (regexp = "^[0-9]{1,4}$" ) String priceMin) { | ||
Map<String, Integer> fruits = Map.of("apple",100,"orange", 150,"grape",1000); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mapの変数名は
keyToValue
valueByKey
のような形式がよいですね。
fruitsToPrice
とかでしょうか。
} | ||
}); | ||
if (fruitsFilterByPrice.isEmpty()){ | ||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
空ならば[]
を返すようにしてもいいかと思います。
Collections.emptyList()
的なものがあったはず。
もしくはfilterした結果をそのまま返せばよいですね。
Integer priceMinToInt = Integer.valueOf(priceMin); | ||
List<String> fruitsFilterByPrice = new ArrayList<>(); | ||
|
||
fruits.forEach((fruitName, fruitPrice) -> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Streamのfilter使ってみるとよいです。
こちら対応あとでよいです↓
こちらですがレスポンスのステータスコードが500になります。 なんでこうなっているの?と思うはずですが、Spring FrameworkのGitHubのIssueでも話が上がっていますね。 じゃあ、どう実装するかですが、このあたりが参考になると思います。 https://www.baeldung.com/spring-boot-bean-validation ただし、こちら優先度は落としてもらっていいです。 |
return namesById.get(Integer.valueOf(id)); | ||
} | ||
|
||
@GetMapping("/fruit") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
複数の果物が返却されるのが予想できるのでfruits
がよいですね。
指摘事項修正(バリデーションのステータスコード対応除く)
ステータスコード対応を除き、いただいた指摘事項対応しました。
|
アレンジした内容
以下2の実行結果を抜粋。