获取ip完整地址 解决This application has no explicit mapping for /error, so you are seeing this as a fallback

问题

今天在写 Java如何获取 IP 属地的博客时,使用谷歌浏览器调用后端获取ip完整地址的接口时,出现了如下图这个错误:

在这里插入图片描述

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Aug 01 15:36:11 CST 2022
There was an unexpected error (type=Internal Server Error, status=500).

status = 500说明是服务器端出现了,服务器端怎么会出现错误呢?于是如下分析

分析问题

获取ip地址的源代码

package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.ServerHttpRequest;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author zs
 * @datetime 2022/8/1 10:30
 * @desc 查询IP地址
 */
@Slf4j
public class IpAddress {
  /**
   * @author zs
   * @datetime 2022/8/1:14:39
   * @desc 获取ip地址
   */
  public static String getIpAddress(ServerHttpRequest request) {
    HttpHeaders headers = request.getHeaders();
    String ipAddress = headers.getFirst("X-Forwarded-For");
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = headers.getFirst("Proxy-Client-IP");
    }
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = headers.getFirst("WL-Proxy-Client-IP");
    }
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getRemoteAddress().getAddress().getHostAddress();
      if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
        // 根据网卡取本机配置的IP
        try {
          InetAddress inet = InetAddress.getLocalHost();
          ipAddress = inet.getHostAddress();
        } catch (UnknownHostException e) {
          log.error("根据网卡获取本机配置的IP异常", e);
        }
      }
    }
    // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
    if (ipAddress != null && ipAddress.indexOf(",") > 0) {
      ipAddress = ipAddress.split(",")[0];
    }
    return ipAddress;
  }
}

接口调用源代码

package com.example.demo.controller;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.example.demo.IpAddress.getIpAddress;

/**
 * @author zs
 * @datetime 2022/7/22 10:14
 * @desc
 */
@RestController
@RequestMapping("/test")
public class IndexController {

  @GetMapping("/getIpAddress")
  public Object get(ServerHttpRequest request) {
    String ipAddress = getIpAddress(request);
    return ipAddress;
  }
}

后端错误

后端报出的错误如下图所示:

在这里插入图片描述

java.lang.IllegalStateException: No primary or single unique constructor found for interface org.springframework.http.server.ServerHttpRequest

从图中可以看到,无法实例化ServerHttpRequest的构造器,原因如下:

  1. 用WebFlux时,获取请求头的信息时使用:ServerHttpRequest

  2. 用MVC时(或WebFlux、MVC依赖同时存在时),获取http请求头的信息使用:HttpServletRequest

而我没有使用WebFlux,因而,我不能使用ServerHttpRequest来接参,所以,应该使用HttpServletRequest来接参。

解决问题

修改获取ip地址的源代码

package com.example.demo;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author zs
 * @datetime 2022/8/1 10:30
 * @desc 查询IP地址
 */
@Slf4j
public class IpAddress {
  /**
   * @author zs
   * @datetime 2022/8/1:14:39
   * @desc 获取ip地址
   */
  public static String getIpAddress(HttpServletRequest request) {

    String ipAddress = request.getHeader("X-Forwarded-For");
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getHeader("Proxy-Client-IP");
    }
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getRemoteAddr();
      if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
        // 根据网卡取本机配置的IP
        try {
          InetAddress inet = InetAddress.getLocalHost();
          ipAddress = inet.getHostAddress();
        } catch (UnknownHostException e) {
          log.error("根据网卡获取本机配置的IP异常", e);
        }
      }
    }

    // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
    if (ipAddress != null && ipAddress.indexOf(",") > 0) {
      ipAddress = ipAddress.split(",")[0];
    }
    return ipAddress;
  }
}

接口调用源代码

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.example.demo.IpAddress.getIpAddress;

/**
 * @author zs
 * @datetime 2022/7/22 10:14
 * @desc
 */
@RestController
@RequestMapping("/test")
public class IndexController {

  @GetMapping("/getIpAddress")
  public Object get(HttpServletRequest request) {
    String ipAddress = getIpAddress(request);
    return ipAddress;
  }
}

从新启动项目,即可获取ip地址了,如下图所示:

在这里插入图片描述

汇总status=404可能出现的错误原因以及解决方案

出现这个异常,说明跳转页面的url无对应的值。

如果以上解决方案无法解决你的问题,可以参考一下解决方案。

解决方案及原因1

Application启动类的位置不对,要将Application类放在最外侧,即包含所有子包 。

原因:spring-boot会自动加载启动类所在包下及其子包下的所有组件.

解决方案及原因2

在springboot的配置文件:application.yml或application.properties中关于视图解析器的配置问题:

  1. 当pom文件下的spring-boot-starter-parent版本高时使用:
    spring.mvc.view.prefix/spring.mvc.view.suffix

  2. 当pom文件下的spring-boot-starter-parent版本低时使用:spring.view.prefix/spring.view.suffix

解决方案及原因3

控制器的URL路径书写问题 :@RequestMapping(“xxxxxxxxxxxxxx”)

实际访问的路径与”xxx”不符合。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

互联网全栈开发实战

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值