spring boot的controller路径解析代码实例@RestController的用法

package com.leju.robot.customer.controller;

import com.leju.robot.common.ResultBean;
import com.leju.robot.common.util.JsonUtils;
import com.leju.robot.customer.common.exception.RobotCustomerExceptionFactor;
import com.leju.robot.customer.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2018/7/20.
 */
@RestController//相当于@controller+@responsebody合起来,如果需要返回的是视图对象,则只需要@controller注解即可
@RequestMapping("/robot")//拦截路径,比如说在本地启动服务则在浏览器中输入的url是:localhost:8080/robot/user
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @RequestMapping(path = "/user",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultBean getUser(){
        LOGGER.info("输入参数是1");
        User user = new User(1,"李四88");
        LOGGER.info("返回是:"+ JsonUtils.toJson(user));
        return new ResultBean(user);
    }

    @RequestMapping(path = "/exception",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultBean getException(){
        LOGGER.info("输入参数是1");
        User user = new User(1,"李四88");
        LOGGER.info("返回是:"+ JsonUtils.toJson(user));
        throw RobotCustomerExceptionFactor.BUILDING_NOT_EXISTS;
    }
}

你可能感兴趣的:(原创)