【发布时间】:2017-10-19 01:08:29
【问题描述】:
是否需要在service层进行另一轮输入验证,非业务逻辑相关?
服务层
@Service
@Transactional
@Validated
public class AppServiceImpl implements AppService {
public App createApp(@Valid App app) { // is there a need to do @Valid here?
return appRepository.save(app);
}
}
控制器层
@RestController
@RequestMapping("/api")
public class AppResource {
private final AppRepository appRepository;
private final AppServiceImpl appServiceImpl;
@Autowired
public AppResource(AppRepository appRepository, AppServiceImpl appServiceImpl) {
this.appServiceImpl = appServiceImpl;
this.appRepository = appRepository;
}
/**
* POST /apps : Create a new app.
*
* @param app the app to create
* @return the ResponseEntity with status 201 (Created) and with body the new app, or with status 400 (Bad Request) if the app has already an ID
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@PostMapping("/apps")
@Timed
public ResponseEntity<App> createApp(@Valid @RequestBody App app) throws URISyntaxException {
log.debug("REST request to save App : {}", app);
if (app.getId() != null) {
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new app cannot already have an ID")).body(null);
}
App result = appServiceImpl.createApp(app);
return ResponseEntity.created(new URI("/api/apps/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
.body(result);
}
}
【问题讨论】:
标签: spring spring-mvc