实训项目记录 | 7.3

7.3.10:14

在发送邮件类SendMess添加自动获取ip和端口方法

java 复制代码
// 获取服务器地址(本机IP + 端口)
    private String getServerAddress() throws Exception {
        if (cachedIpAddr != null) return cachedIpAddr;  // 已经算过直接用

        String ip = null;
        // 遍历所有网卡,找到一个非回环、非虚拟机、非docker的有效IP
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
        while (nics.hasMoreElements()) {
            NetworkInterface nic = nics.nextElement();
            if (!nic.isUp() || nic.isLoopback() || nic.isVirtual()) continue;
            Enumeration<InetAddress> addrs = nic.getInetAddresses();
            while (addrs.hasMoreElements()) {
                InetAddress addr = addrs.nextElement();
                if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress()) {
                    ip = addr.getHostAddress();
                    break;
                }
            }
            if (ip != null) break;
        }
        if (ip == null) ip = InetAddress.getLocalHost().getHostAddress(); // 退而求其次

        cachedIpAddr = "http://" + ip + ":" + serverPort;
        return cachedIpAddr;
    }

获取到的地址进行缓存

java 复制代码
    @Value("${server.port}")
    int serverPort;  // 从配置里取端口
    private String cachedIpAddr; // 缓存结果,避免每次都重新计算

添加静态标签页图标

src/main/resources/static/favicon.ico

避免异常信息

org.springframework.web.servlet.resource.NoResourceFoundException: No static resource favicon.ico.

是浏览器自动请求 /favicon.ico 导致的。

几乎所有现代浏览器在访问页面时,都会尝试请求 http://你的域名/favicon.ico 用于显示浏览器标签页的小图标。这对功能没有任何影响,也不会影响激活流程,它只是个静态资源 404

修改pom

这是一个典型的 NoClassDefFoundError,表明 JVM 在运行时找不到 javax.xml.bind.DatatypeConverter 类。

这个问题通常出现在 Java 9+ 环境中,因为 JAXB API(Java Architecture for XML Binding)

从 Java 9 开始被标记为 deprecated,并在 Java 11 中完全移除。

java 复制代码
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.1</version>
        </dependency>

登录及注册测试成功

获取用户信息测试成功

对logincontroller的getInfo方法作出修改

java 复制代码
    @GetMapping("/getInfo")
//    public Mess getInfo( @RequestHeader("id")Integer id){
    public Mess getInfo(@RequestHeader("Authorization") String token) {
        // 去掉"Bearer "前缀(如果你在Postman里用Bearer Token自动加了)
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        Integer userId = JwtUtils.getMemberIdByJwtToken(token); // 你需要自己实现或确认这个方法
        if (userId == null) {
            return Mess.fail().mess("token无效或已过期");
        }
        return userService.getInfo(userId);
//        return userService.getInfo(id);
    }

使其可以解析token为id

music模块所有功能通过postman测试

需特别注意数据库数据的合法性

有诸多表存在关联关系

比如music和user

歌曲表的歌手id必须在user表存在