NewController.java 1.6 KB
Newer Older
武汉红喜's avatar
武汉红喜 已提交
1
package org.hongxi.whatsmars.earth.web.controller;
武汉红喜's avatar
武汉红喜 已提交
2

3
import org.apache.log4j.Logger;
武汉红喜's avatar
武汉红喜 已提交
4 5
import org.hongxi.whatsmars.common.result.Result;
import org.hongxi.whatsmars.common.result.ResultHelper;
武汉红喜's avatar
武汉红喜 已提交
6
import org.hongxi.whatsmars.earth.service.UserService;
武汉红喜's avatar
武汉红喜 已提交
7
import org.springframework.beans.factory.annotation.Autowired;
武汉红喜's avatar
武汉红喜 已提交
8
import org.springframework.web.bind.annotation.RequestMapping;
武汉红喜's avatar
武汉红喜 已提交
9
import org.springframework.web.bind.annotation.RequestMethod;
武汉红喜's avatar
武汉红喜 已提交
10
import org.springframework.web.bind.annotation.RequestParam;
武汉红喜's avatar
武汉红喜 已提交
11 12
import org.springframework.web.bind.annotation.RestController;

武汉红喜's avatar
武汉红喜 已提交
13
import org.hongxi.whatsmars.earth.domain.pojo.User;
武汉红喜's avatar
武汉红喜 已提交
14 15 16 17 18

/**
 * Created by javahongxi on 2017/10/22.
 */
@RestController
武汉红喜's avatar
武汉红喜 已提交
19
@RequestMapping("/test")
武汉红喜's avatar
武汉红喜 已提交
20 21
public class NewController {

22 23
    private final Logger logger = Logger.getLogger(getClass());

武汉红喜's avatar
武汉红喜 已提交
24 25 26 27 28
    @Autowired
    private UserService userService;

    // localhost:8080/test/add.jhtml
    @RequestMapping(value = "/add", method = RequestMethod.POST)
武汉红喜's avatar
武汉红喜 已提交
29 30 31 32
    public Result add(@RequestParam(name = "name") String username,
                      @RequestParam(required = false) String nickname,
                      @RequestParam(required = false, defaultValue = "1") Integer gender,
                      @RequestParam Integer age) {
33 34 35 36 37 38 39 40 41
        try {
            User user = new User();
            user.setUsername(username);
            user.setNickname(nickname);
            user.setGender(gender);
            user.setAge(age);
            userService.add(user);
        } catch (Exception e) {
            logger.error("##########test add error", e);
武汉红喜's avatar
武汉红喜 已提交
42
            return ResultHelper.newErrorResult();
43 44
        }

武汉红喜's avatar
武汉红喜 已提交
45
        return ResultHelper.newSuccessResult();
武汉红喜's avatar
武汉红喜 已提交
46
    }
武汉红喜's avatar
武汉红喜 已提交
47

武汉红喜's avatar
武汉红喜 已提交
48
}
武汉红喜's avatar
武汉红喜 已提交
49