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<User> getUser(){
        LOGGER.info("输入参数是1");
        User user = new User(1,"李四88");
        LOGGER.info("返回是:"+ JsonUtils.toJson(user));
        return new ResultBean<User>(user);
    }

    @RequestMapping(path = "/exception",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultBean<User> getException(){
        LOGGER.info("输入参数是1");
        User user = new User(1,"李四88");
        LOGGER.info("返回是:"+ JsonUtils.toJson(user));
        throw RobotCustomerExceptionFactor.BUILDING_NOT_EXISTS;
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值