我来自Rails背景,正在深入研究Java。我一直在研究一个在MatchesController.java中定义了show操作的样板项目;索引操作Java Spring Controller
@RestController
final class MatchesController {
private final MatchRepository matchRepository;
@Autowired
MatchesController(MatchRepository matchRepository) {
this.matchRepository = matchRepository;
}
@RequestMapping(method = RequestMethod.GET, value = "/matches/{id}")
ResponseEntity<Match> show(@PathVariable String id) {
Match match = matchRepository.findOne(id);
if (match == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} else {
return new ResponseEntity<>(match, HttpStatus.OK);
}
}
}
在Rails中,show动作看起来像这样;
def show
@match = Match.find(params[:id])
end
该索引动作看起来像;
def index
@matches = Match.all
end
我找我如何用Java编写/春等效指标作用,我觉得我应该定义或使用某种类型的列表或数组对象,以检索所有matchRepository的记录:
我尝试了类似下面的内容,但它当然是错误的,并且不会编译。 show动作确实工作正常,并与我的本地mysql数据库交互很好。我只是一个完整的java/spring新手,并且正在忙碌着。
@RequestMapping(method = RequestMethod.GET, value = "/matches")
ResponseEntity<Match> index() {
Match matches = matchRepository.findAll();
if (matches == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} else {
return new ResponseEntity<>(matches, HttpStatus.OK);
}
}
编译错误;
[ERROR]编译错误:
/Users/home/Latta/Spring/pong_matcher_spring/src/main/java/org/pongmatcher/web/MatchesController.java:[36,48]不兼容的类型:JAVA .util.List不能转换到org.pongmatcher.domain.Match [INFO] 1个错误