You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Spring Boot应用代码更新后未生效,寻求技术帮助

问题解决与SHA-256编码实现

一、修改内容不生效的解决办法

1. 强制重新编译并重启应用

  • 执行Maven命令清理旧编译产物并重新编译:
    mvn clean compile
    
  • 用Maven直接启动应用,确保加载最新代码:
    mvn spring-boot:run
    
    若用IDE开发,先执行Build Project(IntelliJ用Ctrl+F9,Eclipse用Ctrl+B),再重启应用。

2. 排查是否运行旧构建产物

  • 若之前打包过jar包,需重新打包后运行:
    mvn clean package
    java -jar target/sslserver-xxx.jar
    
  • 确认IDE运行配置指向当前项目的最新编译类,而非旧jar或缓存文件。

3. 启用热部署(开发阶段推荐)

在pom.xml中添加Spring Boot DevTools依赖,修改代码后自动重载应用:

<dependencies>
    <!-- 其他原有依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
</dependencies>

添加后重启应用,后续修改代码会自动生效,无需手动重启。

二、SHA-256编码实现

修改ServerController中的myHash方法,完成字符串的SHA-256哈希计算,完整代码如下:

package com.snhu.sslserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@SpringBootApplication
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }

}

@RestController
class ServerController{
    @RequestMapping("/hash")
    public String myHash(){
        String data = "Hello My Name!";
        String sha256Hash = calculateSHA256(data);
        
        return "<p>data: " + data + "</p><p>SHA-256 Hash: " + sha256Hash + "</p>";
    }

    private String calculateSHA256(String input) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hashBytes = digest.digest(input.getBytes());
            // 将字节数组转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : hashBytes) {
                String hex = Integer.toHexString(0xff & b);
                if(hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("SHA-256 algorithm not found", e);
        }
    }
}

代码说明:

  • calculateSHA256方法:通过MessageDigest实现SHA-256加密,将字节数组转换为直观的十六进制字符串返回。
  • 返回值调整:同时展示原始字符串和哈希值,满足作业要求。

运行应用后访问https://localhost:8443/hash,即可看到更新后的字符串和对应的SHA-256哈希值。

内容的提问来源于stack exchange,提问作者Wulfen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 14:35:15