eclipse中导入spring-boot框架的jar包方法

如下代码引入了spring-boot的包

package openresty;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OpenrestySign {

    public static void main(String[] args) throws IOException {
        OpenrestySign.run(OpenrestySign.class, args);
        
        HashMap params = new HashMap();
        
        params.put("key", "1");
        params.put("a", "1");
        params.put("c", "w");
        params.put("b", "2");
        
        long time = new Date().getTime();
        
        params.put("time", "" + time);
        
        System.out.println(time);
        
        String sign = getSignature(params,"123456");
        
        System.out.println(sign);
        
        params.put("sign", sign);
        
        String resp = HttpsUtil.doGet("http://10.11.0.215/sign",params);
        
        System.out.println(resp);
    }
    
    /**
     * 签名生成算法
     * @param HashMap params 请求参数集,所有参数必须已转换为字符串类型
     * @param String secret 签名密钥
     * @return 签名
     * @throws IOException
     */
    public static String getSignature(HashMap params, String secret) throws IOException
    {
        // 先将参数以其参数名的字典序升序进行排序
        Map sortedParams = new TreeMap(params);
        Set> entrys = sortedParams.entrySet();
     
        // 遍历排序后的字典,将所有参数按"key=value"格式拼接在一起
        StringBuilder basestring = new StringBuilder();
        for (Entry param : entrys) {
            if(basestring.length() != 0){
                basestring.append("&");
            }
            basestring.append(param.getKey()).append("=").append(param.getValue());
        }
        basestring.append("&");
        basestring.append(secret);
        
        System.out.println("basestring="+basestring);
     
        // 使用MD5对待签名串求签
        byte[] bytes = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            bytes = md5.digest(basestring.toString().getBytes("UTF-8"));
        } catch (GeneralSecurityException ex) {
            throw new IOException(ex);
        }
        
        String strSign = new String(bytes);
        System.out.println("strSign="+strSign);
        // 将MD5输出的二进制结果转换为小写的十六进制
        StringBuilder sign = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() == 1) {
                sign.append("0");
            }
            sign.append(hex);
        }
        return sign.toString();
    }
}

 

默认直接下载spring-boot的jar包并且引入是不行的,需要新建maven的项目,通过修改pom.xml自动下载依赖

eclipse中导入spring-boot框架的jar包方法_第1张图片

pom.xml内容

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>

  <groupId>openrestygroupId>
  <artifactId>openrestyartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>jarpackaging>

  <name>openrestyname>
  <url>http://maven.apache.orgurl>

  <properties>
    <java.version>1.8java.version>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  properties>


  
  <parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.0.0.RELEASEversion>
parent>

  <dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
        plugin>
    plugins>
build>

<repositories>
    <repository>
        <id>repository.spring.releaseid>
        <name>Spring GA Repositoryname>
        <url>http://repo.spring.io/releaseurl>
    repository>
repositories>
  
project>

 

eclipse中导入spring-boot框架的jar包方法_第2张图片

你可能感兴趣的:(eclipse中导入spring-boot框架的jar包方法)