瑞吉外卖问题(待更新...

2024-06-23 10:44
文章标签 问题 更新 外卖 瑞吉

本文主要是介绍瑞吉外卖问题(待更新...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 一、session注册时
  • 二、用户增加时
  • 三、RequestBody
    • 3.1 Long问题
    • 3.2 RequestBody
    • 3.3 UpdataById


一、session注册时

刚开始使用的是该代码

   httpServletRequest.setAttribute("employee",emp.getId());

导致我点击登录后,还是一直在登录页面,进不去主页面。
原因是:
我使用的那行代码并没有保存到session里,只是保存到当前请求中。当请求别的页面时,就会失效。
正确代码:

httpServletRequest.getSession().setAttribute("employee", emp.getId());

该代码是保存到当前会话域。

移除的代码:

  httpServletRequest.getSession().removeAttribute("employee");

获取代码:

httpServletRequest.getSession().getAttribute("employee")

当时还比较迷惑一个地方,就是 已经已经排除掉以下页面,就是以下页面直接放行。但是当我请求/backend/index.html 时并不会被直接放行,还是需要先登录,经过检查,

 String[] urls=new String[]{"/employee/login","/employee/logout","/backend/**","/front/**"};

原因是:
index.html页面 会直接默认请求一个页面,
在这里插入图片描述
在这里插入图片描述
请求该页面就会被过滤器拦截。

二、用户增加时

ControllerAdvice注解给忘记了,通过该注解可以配置一个全局的信息,其中有一个属性是annotations,: 允许你指定一个或多个类(类型数组),只有继承或实现了这些类的控制器才会受到此 advice 影响。

用途:专门用于处理控制器层的全局异常处理、数据绑定和数据预处理。
应用范围:仅限于 Spring MVC 的控制器层。

原始代码: 使用的是ControllerAdvice

package com.cky.exceptions;import com.cky.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;import java.sql.SQLIntegrityConstraintViolationException;/*** @ClassName MyExceptions* @Description TODO* @Author lukcy* @Date 2024/6/21 15:51* @Version 1.0*/
@Slf4j
@ResponseBody
@ControllerAdvice(annotations = {RestController.class, Controller.class})
public class MyExceptions {/*** 异常处理方法* @param ex* @return*/@ExceptionHandler(SQLIntegrityConstraintViolationException.class)public R<String> EmpExceptions(SQLIntegrityConstraintViolationException ex) {if (ex.getMessage().contains("")) {String[] split = ex.getMessage().split("");String msg = split[2] + "已存在";return R.error(msg);}return R.error("未知错误");}}

修改后
我没有使用该全局异常,而是在保存时先判断是否存在

public interface EmployeeService extends IService<Employee> {boolean isUsernameExist(String username);
}
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {@Autowiredprivate EmployeeMapper employeeMapper;/*** 检查数据库中是否存在指定的用户名** @param username 要检查的用户名* @return 如果存在返回true,否则返回false*/public boolean isUsernameExist(String username) {QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();queryWrapper.eq("username", username);int count = employeeMapper.selectCount(queryWrapper);return count > 0;}
}
   @PostMappingpublic R<String> saveemp(HttpServletRequest httpServletRequest,@RequestBody Employee employee){boolean usernameExist = employeeService.isUsernameExist(employee.getUsername());if (usernameExist){return R.error(String.format("%s 用户名已经存在", employee.getUsername()));}//设置默认初始密码employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));//其他字段employee.setCreateTime(LocalDateTime.now());employee.setUpdateTime(LocalDateTime.now());Long id = (Long) httpServletRequest.getSession().getAttribute("employee");employee.setCreateUser(id);employee.setUpdateUser(id);//直接调用接口的save方法employeeService.save(employee);return R.success("员工添加成功");}

能够实现同样的功能
在这里插入图片描述

三、RequestBody

在修改用户状态时:

3.1 Long问题

js 在对Long数据传送时,只会保证前16位有效,做法:增加一个自己的消息转换器+对象转换器,把Long数据转换为String。

package com.cky.common;import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;/*** 对象映射器:基于jackson将Java对象转为json,或者将json转为Java对象* 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]* 从Java对象生成JSON的过程称为 [序列化Java对象到JSON]*/
public class JacksonObjectMapper extends ObjectMapper {public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";public JacksonObjectMapper() {super();//收到未知属性时不报异常this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);//反序列化时,属性不存在的兼容处理this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);SimpleModule simpleModule = new SimpleModule().addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT))).addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT))).addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT))).addSerializer(BigInteger.class, ToStringSerializer.instance).addSerializer(Long.class, ToStringSerializer.instance).addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT))).addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT))).addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));//注册功能模块 例如,可以添加自定义序列化器和反序列化器this.registerModule(simpleModule);}
}
package com.cky.config;import com.cky.common.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.cbor.MappingJackson2CborHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import java.util.List;/*** @ClassName WebmvcConfig* @Description TODO* @Author lukcy* @Date 2024/5/29 9:40* @Version 1.0*/
//默认能访问类路径下的static和template文件夹 这里我们没有放在这些文件夹下 所以访问不到,需要自己配置静态资源处理器
@Configuration
@Slf4j
public class WebmvcConfig extends WebMvcConfigurationSupport {/*** 静态资源映射* @param registry*/@Overrideprotected void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");}/*** 增加消息转换器* @param converters*/@Overrideprotected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {log.info("配置的消息转换器....");MappingJackson2HttpMessageConverter mappingJackson2CborHttpMessageConverter=new MappingJackson2HttpMessageConverter();mappingJackson2CborHttpMessageConverter.setObjectMapper(new JacksonObjectMapper());converters.add(0,mappingJackson2CborHttpMessageConverter);}
}

3.2 RequestBody

刚开始在控制器上没有加上@RequestBody注解,导致传入的参数并不能赋给Employee类。
RequestBody和RequestParam区别
@RequestBody会将请求体中的数据,转换成对象

@RequestParam会从http请求查询参数中提取数据

@RequestParam和@RequestBody是Spring Framework中用于处理HTTP请求的注解,它们有以下区别:

1.数据来源:

@RequestParam: 从HTTP请求的查询参数中提取数据,即从URL中的?key=value形式的参数中获取数据。
@RequestBody: 从HTTP请求的请求体(body)中提取数据,通常用于接收JSON、XML等格式的数据。
2.用法:

@RequestParam: 通常用于处理GET请求或POST请求中的表单数据,例如?name=John&age=30这样的查询参数。
@RequestBody: 通常用于处理POST请求中的非表单数据,例如JSON格式的数据,或者XML格式的数据。
3.数据格式:

@RequestParam: 提取的数据一般是简单类型,如字符串、整数等。
@RequestBody: 提取的数据可以是复杂类型,如自定义的Java对象、Map、List等,通常是用于反序列化JSON或XML数据为Java对象。

3.3 UpdataById

由于Mybatis plus默认的更新策略是NOT_NULL:非 NULL;即通过接口更新数据时数据为NULL值时将不更新进数据库。所以Mybatis plus通过updateById(XXX)更新数据,当用户有更新字段为 空字符串 或者 null 的需求时,需要对 FieldStrategy 策略进行调整。

FieldStrategy 有三种策略:

IGNORED:0 忽略
NOT_NULL:1 非 NULL,默认策略
NOT_EMPTY:2 非空

这篇关于瑞吉外卖问题(待更新...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1086932

相关文章

解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题

《解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题》在Spring开发中,@Autowired注解常用于实现依赖注入,它可以应用于类的属性、构造器或setter方法上,然... 目录1. 为什么 @Autowired 在属性上被警告?1.1 隐式依赖注入1.2 IDE 的警告:

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

关于Nginx跨域问题及解决方案(CORS)

《关于Nginx跨域问题及解决方案(CORS)》文章主要介绍了跨域资源共享(CORS)机制及其在现代Web开发中的重要性,通过Nginx,可以简单地解决跨域问题,适合新手学习和应用,文章详细讲解了CO... 目录一、概述二、什么是 CORS?三、常见的跨域场景四、Nginx 如何解决 CORS 问题?五、基

MySQL安装时initializing database失败的问题解决

《MySQL安装时initializingdatabase失败的问题解决》本文主要介绍了MySQL安装时initializingdatabase失败的问题解决,文中通过图文介绍的非常详细,对大家的学... 目录问题页面:解决方法:问题页面:解决方法:1.勾选红框中的选项:2.将下图红框中全部改为英

Nginx启动失败:端口80被占用问题的解决方案

《Nginx启动失败:端口80被占用问题的解决方案》在Linux服务器上部署Nginx时,可能会遇到Nginx启动失败的情况,尤其是错误提示bind()to0.0.0.0:80failed,这种问题通... 目录引言问题描述问题分析解决方案1. 检查占用端口 80 的进程使用 netstat 命令使用 ss

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

pip install jupyterlab失败的原因问题及探索

《pipinstalljupyterlab失败的原因问题及探索》在学习Yolo模型时,尝试安装JupyterLab但遇到错误,错误提示缺少Rust和Cargo编译环境,因为pywinpty包需要它... 目录背景问题解决方案总结背景最近在学习Yolo模型,然后其中要下载jupyter(有点LSVmu像一个