1、封装的请求对象
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CourseInfosRequest {
private List<Long> ids;
}
2、Controller
/**
* 1、以集合作为参数传递 { "ids":[8,9]}
* 2、请求方式为:POST
*3、请求路径:
* @param request
* @return
*/
@PostMapping("/get/courses")
public List<CourseInfo> getCourseInfos(
@RequestBody CourseInfosRequest request) {
log.info("<homepage-course>: get courses -> {}",
JSON.toJSONString(request));
return courseService.getCourseInfos(request);
}
3、Service实现
@Override
public List<CourseInfo> getCourseInfos(CourseInfosRequest request) {
if(CollectionUtils.isEmpty(request.getIds()))
{
return Collections.emptyList();
}
List<HomepageCourse> homepageCourses = homepageCourseDao.findAllById(request.getIds());
return homepageCourses.stream()
.map(this::buildCourseInfo)
.collect(Collectors.toList());
}
private CourseInfo buildCourseInfo(HomepageCourse course){
return CourseInfo.builder()
.id(course.getId())
.courseName(course.getCourseName())
.courseType(course.getCourseType()==0?"免费课程":"实战课程")
.courseIcon(course.getCourseIntro())
.courseIntro(course.getCourseIntro())
.build();
}
4、主要引用类型
5、测试结果