@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaTypes.JSON) // 按Restful风格约定,返回204状态码, 无内容. 也可以返回200状态码. @ResponseStatus(HttpStatus.NO_CONTENT) public void update(@RequestBody Curriculumbase curriculumbase) { // 调用JSR303 Bean Validator进行校验, 异常将由RestExceptionHandler统一处理. BeanValidators.validateWithException(validator, curriculumbase); // 保存更新 curriculumbaseService.updateCurriculumbase(curriculumbase); }
@Log(message = "修改了{0}用户的信息。") @RequiresPermissions("User:edit") @RequestMapping(value = "/update", method = RequestMethod.POST) public @ResponseBody String update(@ModelAttribute("preloadUser") User user) { BeanValidators.validateWithException(validator, user); userService.update(user); LogUitl.putArgs(LogMessageObject.newWrite().setObjects(new Object[] {user.getUsername()})); return AjaxObject.newOk("修改用户成功!").toString(); }
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaTypes.JSON) // 按Restful风格约定,返回204状态码, 无内容. 也可以返回200状态码. @ResponseStatus(HttpStatus.NO_CONTENT) public void update(@RequestBody Task task) { // 调用JSR303 Bean Validator进行校验, 异常将由RestExceptionHandler统一处理. BeanValidators.validateWithException(validator, task); // 保存任务 taskService.saveTask(task); }
/** 处理JSR311 Validation异常. */ @ExceptionHandler(value = {ConstraintViolationException.class}) public final ResponseEntity<?> handleException( ConstraintViolationException ex, WebRequest request) { Map<String, String> errors = BeanValidators.extractPropertyAndMessage(ex.getConstraintViolations()); String body = jsonMapper.toJson(errors); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType(MediaTypes.TEXT_PLAIN_UTF_8)); return handleExceptionInternal(ex, body, headers, HttpStatus.BAD_REQUEST, request); }
@Log(message = "添加了{0}用户。") @RequiresPermissions("User:save") @RequestMapping(value = "/create", method = RequestMethod.POST) public @ResponseBody String create(User user) { BeanValidators.validateWithException(validator, user); user.setCreateTime(new Date()); try { userService.save(user); } catch (ExistedException e) { return AjaxObject.newError(e.getMessage()).setCallbackType("").toString(); } LogUitl.putArgs(LogMessageObject.newWrite().setObjects(new Object[] {user.getUsername()})); return AjaxObject.newOk("添加用户成功!").toString(); }
@RequestMapping(method = RequestMethod.POST, consumes = MediaTypes.JSON) public ResponseEntity<?> create(@RequestBody Task task, UriComponentsBuilder uriBuilder) { // 调用JSR303 Bean Validator进行校验, 异常将由RestExceptionHandler统一处理. BeanValidators.validateWithException(validator, task); // 保存任务 taskService.saveTask(task); // 按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象. Long id = task.getId(); URI uri = uriBuilder.path("/api/v1/task/" + id).build().toUri(); HttpHeaders headers = new HttpHeaders(); headers.setLocation(uri); return new ResponseEntity(headers, HttpStatus.CREATED); }
@RequestMapping(method = RequestMethod.POST, consumes = MediaTypes.JSON) public ResponseEntity<?> create( @RequestBody Curriculumbase curriculumbase, UriComponentsBuilder uriBuilder) { // 调用JSR303 Bean Validator进行校验, 异常将由RestExceptionHandler统一处理. BeanValidators.validateWithException(validator, curriculumbase); // 保存新增 curriculumbaseService.saveCurriculumbase(curriculumbase); // 按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象. String id = curriculumbase.getId(); URI uri = uriBuilder.path("/curriculumbase/" + id).build().toUri(); HttpHeaders headers = new HttpHeaders(); headers.setLocation(uri); return new ResponseEntity(headers, HttpStatus.CREATED); }
/** @see AccountWebService#createUser() */ @Override public IdResult createUser(UserDTO user) { try { User userEntity = BeanMapper.map(user, User.class); Long userId = accountManager.saveUser(userEntity); return new IdResult(userId); } catch (ConstraintViolationException e) { String message = StringUtils.join(BeanValidators.extractPropertyAndMessage(e), "\n"); return new IdResult().setError(WSResult.PARAMETER_ERROR, message); } catch (DataIntegrityViolationException e) { String message = "新建用户参数存在唯一性冲突(用户:" + user + ")"; logger.error(message, e); return new IdResult().setError(WSResult.PARAMETER_ERROR, message); } catch (RuntimeException e) { logger.error(e.getMessage(), e); return new IdResult().setDefaultError(); } }