Package org.jeecgframework.web.rest.controller

Source Code of org.jeecgframework.web.rest.controller.UserRestController

package org.jeecgframework.web.rest.controller;

import java.net.URI;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.UserService;

import org.jeecgframework.core.beanvalidator.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.util.UriComponentsBuilder;

/**
* TSUser的Restful API的Controller.
*
* @author liuht
*/
@Controller
@RequestMapping(value = "/user")
public class UserRestController {

  @Autowired
  private UserService userService;

  @Autowired
  private Validator validator;

  @RequestMapping(method = RequestMethod.GET)
  @ResponseBody
  public List<TSUser> list() {
    List<TSUser> listUsers=userService.getList(TSUser.class);
    return listUsers;
  }

  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  @ResponseBody
  public ResponseEntity<?> get(@PathVariable("id") Long id) {
    TSUser task = userService.get(TSUser.class, id);
    if (task == null) {
      return new ResponseEntity(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity(task, HttpStatus.OK);
  }

  @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  public ResponseEntity<?> create(@RequestBody TSUser user, UriComponentsBuilder uriBuilder) {
    //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
    Set<ConstraintViolation<TSUser>> failures = validator.validate(user);
    if (!failures.isEmpty()) {
      return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
    }

    //保存用户
    userService.save(user);

    //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
    String id = user.getId();
    URI uri = uriBuilder.path("/rest/user/" + id).build().toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(uri);

    return new ResponseEntity(headers, HttpStatus.CREATED);
  }

  @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<?> update(@RequestBody TSUser user) {
    //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
    Set<ConstraintViolation<TSUser>> failures = validator.validate(user);
    if (!failures.isEmpty()) {
      return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
    }

    //保存
    userService.saveOrUpdate(user);

    //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
    return new ResponseEntity(HttpStatus.NO_CONTENT);
  }

  @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  @ResponseStatus(HttpStatus.NO_CONTENT)
  public void delete(@PathVariable("id") Long id) {
    userService.deleteEntityById(TSUser.class, id);
  }
}
TOP

Related Classes of org.jeecgframework.web.rest.controller.UserRestController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.