Controller接收参数三种方法

3.字段名与实体类的属性名不一样,记得开驼峰命名,要不然实体类就不能封装,或手动映射

mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true

4.什么是反向代理

一种网络架构技术,通过反向代理服务器为后端服务器做代理(安全,灵活,负载均衡)

Controller接收参数三种方法

方法1 :通过原始的HttpServletRequest对象获取参数,繁琐,手动类型转换

 @DeleteMapping("/users")
    public Result deleteUserById(HttpServletRequest request) {
        String id = request.getParameter("id");
        int idInt = Integer.parseInt(id);
     //   userService.deleteUserById( idInt);
        System.out.println("根据id删除人员" + idInt);
        return   Result.success();
    }

方法2 :通过RequestParam获取参数  ,默认required为true 一当声明参数,改参数必须要传递,否则报错

@DeleteMapping("/users")
    public Result deleteUserById(@RequestParam(value = "id" , required =false)Integer  id ) {
        System.out.println("根据id删除人员" + id);
        return   Result.success();
    }

方法3:如果请求参数与形参变量名相同,直接定义方法形参可以接收(省略@ReuestParam)

@RequestBody 接收客户端发送的JSON格式数据,并自动将其转换为Java对象。

@PathVariable 注解用于从 URL 路径中提取变量值。

路径参数:通过URL直接传递参数,使用{。。。},来识别改路经参数,需要@PathVariable

@getMapper("/depts/{id}")
public Result GetInfo(@PathVariable("id")Integer Id){

        
}

可以加在controller上表示路径

@RequestMapping("/users")

日志技术

想用的话,直接加注释@Slf4j

日志级别 追踪 debug调试 info 记录信息 warn 警告  error错误

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- 控制台输出 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d 表示日期,%thread 表示线程名,%-5level表示级别从左显示5个字符宽度,%logger显示日志记录器的名称, %msg表示日志消息,%n表示换行符 -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- 系统文件输出 -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- 日志文件输出的文件名, %i表示序号 -->
            <FileNamePattern>/usr/local/tlias-%d{yyyy-MM-dd}-%i.log</FileNamePattern>
            <!-- 最多保留的历史日志文件数量 -->
            <MaxHistory>30</MaxHistory>
            <!-- 最大文件大小,超过这个大小会触发滚动到新文件,默认为 10MB -->
            <maxFileSize>10MB</maxFileSize>
        </rollingPolicy>

        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d 表示日期,%thread 表示线程名,%-5level表示级别从左显示5个字符宽度,%msg表示日志消息,%n表示换行符 -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}-%msg%n</pattern>
        </encoder>
    </appender>

    <!-- 日志输出级别   大于对于INFO才会输出-->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

全局异常处理器

@RestControllerAdvice   获取全局异常,返回json

@ExceptionHandler  捕获特定异常执行方法

package com.itheima.exception;

import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public Result handleException(Exception e){
        log.error("程序出错啦~", e);
        return Result.error("出错啦, 请联系管理员~");
    }

    @ExceptionHandler
    public Result handleDuplicateKeyException(DuplicateKeyException e){
        log.error("程序出错啦~", e);
        String message = e.getMessage();
        int i = message.indexOf("Duplicate entry");
        String errMsg = message.substring(i);
        String[] arr = errMsg.split(" ");
        return Result.error( arr[2] + " 已存在");
    }

    /**
     * 声明异常处理的方法 - BusinessException
     */
    @ExceptionHandler
    public Result handleBuinessException(BusinessException businessException) {
        log.error("服务器异常", businessException);
        return Result.error(businessException.getMessage());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值