當前項目前后端都在一起時,避免一個一個controller的寫使用配置文件進行映射文章來源地址http://www.zghlxwxcb.cn/news/detail-527930.html
package com.example.ruiji.Config;
import com.example.ruiji.Interceptor.LoginInterceptor;
import com.example.ruiji.commom.JacksonObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
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.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.util.List;
@Configuration
//為了解決 WebMvcConfigurationSupport和 Spring Boot 2.0版本以后,官方不再推薦使用WebMvcConfigurationSupport,取而代之的是使用WebMvcConfigurer接口。因此,如果您使用了繼承自WebMvcConfigurationSupport的配置類1
public class webMvc extends WebMvcConfigurationSupport {
/**
* 在mvc 框架中設(shè)置靜態(tài)資源目錄映射 對mvc 路徑進行映射
* @param registry
*/
@Autowired
StringRedisTemplate redisTemplate;
@Autowired
LoginInterceptor loginInterceptor;
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
//訪問這個路徑 映射到對應(yīng)的靜態(tài)資源目錄 就不需要一個個的controller寫跳轉(zhuǎn)
registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/template/backend/");
registry.addResourceHandler("/front/**").addResourceLocations("classpath:/template/front/");
super.addResourceHandlers(registry);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor).excludePathPatterns(
"/backend/**",
"/front/**",
"/employee/login",
"/user/login",
"backend/page/login/**",
"/employee/logout/**",
"/common/**",// 因為前端是通過action直接上傳 并不不axios發(fā)的 沒有authentic 肯定會被攔截
//因為前端攔截器失效 所以后端攔截器放行我自己寫的驗證登錄狀態(tài)接口 來給前端響應(yīng)式跳轉(zhuǎn)
// 前端除去個人信息不攔截
"/category/list",
"/shoppingCart/list",
"/setmeal/list",
"/dish/list",
"/shoppingCart/add"
);//不用在new 攔截器 并且需要配置攔截路徑 避免全部看攔截
}
/**
* 擴展默認轉(zhuǎn)換器
* 來替代默認的springmvc的轉(zhuǎn)換器
* @param converters the list of configured converters to extend
*/
// @Override
// protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
// MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
設(shè)置對象轉(zhuǎn)換器 自定義的
// mappingJackson2HttpMessageConverter.setObjectMapper(new JacksonObjectMapper());
// // 上寫的轉(zhuǎn)換器添加到轉(zhuǎn)換器鏈中
// converters.add(0,mappingJackson2HttpMessageConverter);
//
// }
}
文章來源:http://www.zghlxwxcb.cn/news/detail-527930.html
到了這里,關(guān)于Springboot前后端不分離的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!