Spring Boot中处理404错误的解决方案

Java 专栏收录该内容
¥59.90 ¥99.00
34篇文章

在使用Spring Boot开发Web应用程序时,404错误是常见的问题之一。当请求的资源不存在时,服务器会返回404错误。本文将介绍如何在Spring Boot中处理404错误,并提供相应的源代码示例。

一、配置错误页面

要处理404错误,首先需要配置一个自定义的错误页面。在Spring Boot中,可以通过创建一个实现ErrorController接口的类来实现自定义错误页面的配置。以下是一个示例:

import org.springframework.boot.web.servlet.error.ErrorController;
import</
2.spring系列之404异常的捕获 回顾 我在之前发布了一篇spring统一返回的文章,最后提到是无法捕获404异常的,这里我们先来测试一下 @RestController public class TestController { @GetMapping("/test") public String insert22() { return "hello"; } } 浏览器请求试一下 http://localhost:8080/xxx 报错 # Whitelabel Error Page This 阅读详情

相关推荐

spring boot最新教程(五):404错误500错误统一处理

Spring Boot默认使用嵌入式Tomcat,默认没有页面来处理404等常见错误。因此,为了给用户最佳的使用体验,404,500等常见错误需要我们自定义页面来处理。 我们需要用org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer类来实现该功能,在Spring Boot的配置类(@Confi

御前两把刀的专栏 2万+

Spring Boot 进阶-Spring Boot的全局异常处理机制详解

其实在Spring 3.x的版本中就已经有全局异常处理的注解被提出使用了。例如@ControllerAdvice、@ExceptionHandler、@InitBinder、@ModelAttribute 等等注解。而这些注解从字面上来看与异常有关的就只有一个@ExceptionHandler 注解,也就是异常处理器的关键注解。其实在Spring中的异常处理机制有两种,一种是全局异常处理、一种是局部异常处理。那么什么是全局异常处理?什么是局部异常处理?

初学者 2478

Spring Boot 2 处理 404 500 异常

项目可能会有各种未知异常,一旦出现异常就会把堆栈信息暴露给前端,所以有个地方统一处理所有异常情况。 继承spring boot 2.0.0 才有的接口 ErrorController 。 /** * @author xxx */ @RestController public class SystemExceptionController implements ErrorContro...

Dangerous 500

spring boot 整合 jsp 访问报错 404

具体步骤: 创建spring boot项目只勾选 web 组件即可(本测试项目)。打包方式为war包,后再pom文件中添加支持外部Tomcat依赖包 &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w...

依着风睡的博客 2985

spring boot 下 500 404 403错误页面处理

spring Boot 下http 500 404 403错误页面处理。同时文章是在《SpringBoot接口服务处理Whitelabel Error Page》基础上新增内容,更加详细的说明请参考这篇文章。本文直说明差异部分。

loongshawn的博客 8528

Spring Boot集成Swagger/Knife4j遇404?一文详解排查与解决

在Java后端开发中,API文档自动化是提升团队协作效率的关键技术。通过自动生成接口说明、参数模型和在线调试功能,开发者能够确保文档与代码的实时同步。其核心原理在于利用注解驱动和静态资源映射机制,将API元数据转化为可视化界面。这一技术显著降低了维护成本,并广泛应用于微服务架构、前后端分离等场景。实践中,集成Swagger或Knife4j时,常因**静态资源路径被拦截**或**版本兼容性问题**导致UI页面无法访问。本文聚焦这些高频痛点,系统梳理从基础配置到高级排查的完整解决方案,帮助开发者快速定位并修复4

weixin_30325487的博客 335

Spring Boot Swagger 404问题全解析:从原理到实战解决

在Java Web开发中,API文档是前后端协作和接口测试的关键。Swagger作为一套基于OpenAPI规范的API文档生成工具,通过自动扫描代码注解,能动态生成交互式文档界面,极大提升了开发效率。其核心原理在于,Spring Boot的自动配置机制会注册特定的资源处理器,将请求路径映射到jar包内的静态资源文件。然而,在实际工程实践中,开发者常因配置不当或环境冲突,导致Swagger UI页面无法访问,出现令人困惑的404错误。这通常涉及路径映射、静态资源处理、以及安全拦截等多个技术环节。尤其在微服务架

weixin_30861797的博客 355

Spring Boot 自定义错误页面与异常处理

在真实的Web应用中,用户可能遇到404(页面找不到)、500(服务器内部错误)等问题。对普通用户不友好(显示技术术语)无法展示企业品牌(如LOGO、自定义提示语)前后端分离场景需要返回JSON格式错误信息自定义HTML错误页面(适配404、500等状态码)全局异常处理器(@ControllerAdvice)捕获业务异常区分API请求和页面请求返回不同格式(HTML/JSON)自定义错误信息(添加额外业务字段)

架构师的AI之路,分享AI应用开发架构的学习与实践。 1091

那些在《JavaEE开发的颠覆者 Spring Boot实战》中遇到的坑,,。(一)

一、一开始下了一本PDF书,影印版,看一般的字还是看的清,但是看到代码部分的话,还是会有模糊,尤其是一些配置的时候,后来是在不行就去网上搜了一下,找到一个网易云阅读上有网页版的《JavaEE开发的颠覆者 Spring Boot实战》,刚想感谢网易爸爸,才发现他不能选中,但还是将就着用了一会儿,毕竟比影印版清晰多了。然而,一个偶然的机会,发现这个网易右键没反应,有问题,按个"F12"试试,于是就找到...

qq_34652458的博客 6441

Vue.js 刷新路径404问题解决方案

通过正确地配置后端服务器、Vue.js 路由和服务器部署,你应该能够解决 Vue.js 刷新路径出现的404错误问题。确保后端服务器始终返回同一个 HTML 页面,并正确地配置 Vue.js 路由,以便在刷新页面时能够正确匹配到路由。在部署过程中,我们需要将 Vue.js 项目的静态文件正确地放置在服务器的指定目录下,并配置服务器以正确地响应这些静态文件请求。在这个文件中,我们需要使用正确的路径配置路由,以便在刷新页面时能够正确匹配到对应的组件。其次,我们需要确保 Vue.js 的路由配置正确无误。

2301_79365003的博客 507

Spring Boot:异常处理

Spring Boot异常处理介绍

啊Q老师 6849

Spring Boot 实现异常统一处理接口处理404、500等错误实现跳转指定页面

1、自定义一个Controller需要实现ErrorController接口,重写handleError方法。 package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servl...

蔡定努 1万+

使用嘉立创绘制STM32F103T8C6最小系统板

使用嘉立创EDA绘制一个属于自己的STM32最小系统板

weixin_67887035的博客 1万+

Vue3 + OpenLayers 开发指南(一) - 环境搭建与基础配置

本文详细介绍了如何从零开始搭建Vue3与OpenLayers的集成开发环境。通过项目初始化、核心依赖安装、目录结构规划及基础配置,手把手引导开发者创建一个结构清晰、可维护的现代化WebGIS应用骨架,为后续添加地图图层和交互功能奠定坚实基础。

weixin_28049429的博客 339

GD32H7 SPI驱动实战:手把手教你用SPI3连接外部Flash(W25Q128)并实现读写

本文详细介绍了基于GD32H7系列MCU的SPI3接口驱动W25Q128外部Flash的全流程开发指南。从硬件设计、引脚配置到SPI3外设深度配置,再到底层通信函数实现和高级功能优化,提供了完整的解决方案。特别针对高速读取、DMA传输和信号完整性等关键环节进行了优化,确保系统在-40℃~85℃范围内稳定工作。

weixin_28729205的博客 380

NX二次开发新建组件

NXOpen新建组件

qq_39541524的博客 329

基于FPGA设计的信号分析仪(项目资料ID:28)

基于现场可编程门阵列(FPGA)设计的信号分析仪。该信号分析仪旨在实现对各类信号的精确分析,具备频率、幅度、相位等参数测量功能。通过采用 FPGA 作为核心处理单元,充分利用其并行处理能力和可重构特性,提高了系统的处理速度和灵活性。论文介绍了信号分析仪的总体设计方案,包括硬件电路设计和软件算法实现。

以培养嵌入式工程师为己任 1万+

YOLOv8改进策略【卷积层】| GnConv:一种通过门控卷积和递归设计来实现高效、可扩展、平移等变的高阶空间交互操作

HorNet: 利用递归门控卷积实现高效高阶空间交互GnConv(Recursive Gated Convolution,递归门控卷积)

Limiiiing的博客 324

09 SAP CPI 访问公共API接口

我在网上找了一个公共API,入参输入汉字返回拼音。至此,一个真正的访问API的接口就做好了。点击保存时可看到红叉报错。

SAP CPI 477

Python爬虫实战:东方财富股吧与雪球舆情数据实时爬取与情绪指标挖掘

本文详细介绍如何使用Python爬虫技术实时爬取东方财富股吧和雪球平台的讨论数据,结合股价波动构建情绪分析指标。我们将使用最新的异步爬虫技术、自然语言处理方法和可视化工具,为量化交易提供数据支持。文章包含完整的代码实现和技术细节,适合有一定Python基础的读者学习。

2201_76125261的博客 1224

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

上一篇: Hibernate 跨数据库,JSON 字段处理方案,自定义扩展 JsonStringType Java
下一篇: 寻找字符串中第n个仅出现m次的字符算法(Java实现)
普通网友
博客等级 码龄3年 89粉丝 258原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值