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

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

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

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

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

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

    // localhost:8080/test/add.jhtml
    @RequestMapping(value = "/add", method = RequestMethod.POST)
武汉红喜's avatar
武汉红喜 已提交
30 31 32 33
    public Result add(@RequestParam(name = "name") String username,
                      @RequestParam(required = false) String nickname,
                      @RequestParam(required = false, defaultValue = "1") Integer gender,
                      @RequestParam Integer age) {
34 35 36 37 38 39 40 41 42
        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
武汉红喜 已提交
43
            return ResultHelper.newErrorResult();
44 45
        }

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

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