Commit 91f82d078f5a4ca7e007a22597f0e1650a173fb5

Authored by zhongnanhuang
1 parent 7aea020c

官网商品信息路由,404默认路由

shop/src/main/java/com/canrd/shop/advice/ErrorControllerAdvisor.java 0 → 100644
  1 +package com.canrd.shop.advice;
  2 +
  3 +import org.springframework.web.bind.annotation.ControllerAdvice;
  4 +import org.springframework.web.bind.annotation.ExceptionHandler;
  5 +import org.springframework.web.servlet.NoHandlerFoundException;
  6 +
  7 +/**
  8 + * @author zhongnanhuang
  9 + * @version 1.0
  10 + * @project canrd-services
  11 + * @description 错误处理
  12 + * @date 2024/5/30 11:20:50
  13 + */
  14 +@ControllerAdvice
  15 +public class ErrorControllerAdvisor {
  16 +
  17 + /**
  18 + * 404重定向至首页
  19 + * @param ex
  20 + * @return
  21 + */
  22 + @ExceptionHandler(NoHandlerFoundException.class)
  23 + public String handle404(Exception ex) {
  24 +
  25 + return "redirect:http://www.canrud.com";//this is view name
  26 +
  27 + }
  28 +
  29 +}
... ...
shop/src/main/java/com/canrd/shop/config/WebConfig.java
... ... @@ -3,7 +3,12 @@ package com.canrd.shop.config;
3 3 import com.alibaba.fastjson.support.config.FastJsonConfig;
4 4 import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
5 5 import lombok.extern.slf4j.Slf4j;
  6 +import org.springframework.boot.web.server.ErrorPage;
  7 +import org.springframework.boot.web.server.WebServerFactoryCustomizer;
  8 +import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
  9 +import org.springframework.context.annotation.Bean;
6 10 import org.springframework.context.annotation.Configuration;
  11 +import org.springframework.http.HttpStatus;
7 12 import org.springframework.http.MediaType;
8 13 import org.springframework.http.converter.HttpMessageConverter;
9 14 import org.springframework.http.converter.StringHttpMessageConverter;
... ... @@ -81,4 +86,5 @@ public class WebConfig implements WebMvcConfigurer {
81 86 fastConverter.setDefaultCharset(StandardCharsets.UTF_8);
82 87 return fastConverter;
83 88 }
  89 +
84 90 }
... ...
shop/src/main/java/com/canrd/shop/controller/RedirectController.java 0 → 100644
  1 +package com.canrd.shop.controller;
  2 +
  3 +import org.springframework.stereotype.Controller;
  4 +import org.springframework.web.bind.annotation.GetMapping;
  5 +import org.springframework.web.bind.annotation.RequestMapping;
  6 +import org.springframework.web.bind.annotation.RequestParam;
  7 +
  8 +/**
  9 + * @author zhongnanhuang
  10 + * @version 1.0
  11 + * @project canrd-services
  12 + * @description 国内官网请求重定向
  13 + * @date 2024/5/30 10:03:13
  14 + */
  15 +@RequestMapping("/")
  16 +@Controller
  17 +public class RedirectController {
  18 +
  19 + @GetMapping("/shop/product/getProductById")
  20 + public String redirectToShop(@RequestParam("id") String id) {
  21 + if (id == null){
  22 + return "redirect:http://www.canrud.com/";
  23 + }
  24 + return "redirect:http://www.canrud.com/products/detail/"+id;
  25 + }
  26 +}
... ...
shop/src/main/resources/application.yml
... ... @@ -3,4 +3,8 @@ server:
3 3  
4 4 spring:
5 5 profiles:
6   - active: test
7 6 \ No newline at end of file
  7 + active: test
  8 + mvc:
  9 + throw-exception-if-no-handler-found: true
  10 + resources:
  11 + add-mappings: false
... ...