栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2022-03-09 json接口 idea maven mongo springboot pom配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2022-03-09 json接口 idea maven mongo springboot pom配置

目录

1.关于配置文件

2.关于启动类配置 不启动数据库 

3.关于返回json

4.关于配置文件

5.关于Mongo连接

6.关于配置文件读取

7.打包成jar包 启动 java -jar 文件位置


1.关于配置文件


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
         
    
    com.example
    test
    0.0.1-SNAPSHOT
    test
    
    jar
    Demo project for Spring Boot
    
        1.8
        true
    

    

        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework
            spring-core
            5.3.6
        
        
            com.vaadin.external.google
            android-json
            0.0.20131108.vaadin1
            compile
        

        
        
            org.mongodb
            bson
            3.10.1
        
        
            org.mongodb
            mongo-java-driver
            3.10.1
        
        
            org.mongodb
            mongodb-driver-legacy
            3.10.1
        
        
            org.springframework.data
            spring-data-mongodb
            2.0.5.RELEASE
        

        
        
            org.projectlombok
            lombok
        

    

    
        
            
            
                org.apache.maven.plugins
                maven-resources-plugin
                3.1.0
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.3.1.RELEASE
                
                    com.example.test.testdemo.TestApplication
                
                
                    
                        
                            repackage
                        
                    
                
            

            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                2.22.2
                
                    true
                
            

        
    


2.关于启动类配置 不启动数据库 
@SpringBootApplication(scanbasePackages = "com",exclude = { DataSourceAutoConfiguration.class , MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
public class TestApplication {

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

}

3.关于返回json
//@Controller  注册Controller对象
@RestController // @Controller与 ResponseBody合二为一注解
@RequestMapping(path = "/") // 地址值
public class TestController {

    @Resource
    private TestService testService;

    
    @RequestMapping("/getUser")
    public String welcome(){
        return "Crud Spring Boot Project ! ";
    }

    
    @RequestMapping("/getUser1")
    public List> testlist() throws IOException {
        //获取信息
        List testlist= testService.getTestList();
        //提取信息
        List> list = new ArrayList>();
        if(testlist.size()>0){
            for(int i=0;i map = new HashMap();
                map.put("icode",document.get("icode"));
                map.put("iname",document.get("iname"));
                list.add(map);
            }
        }
        return list;
    }

}

4.关于配置文件
# 配置端口
server.port: 8082

5.关于Mongo连接
List resultMongodb=new ArrayList<>();

String mongodbUserName = getProperties("jdbc.porperties","mongodb_userName");
String mongodbDatabase =  getProperties("jdbc.porperties","mongodb_database");
String mongodbPassword = getProperties("jdbc.porperties","mongodb_password");
String mongodbAddress =  getProperties("jdbc.porperties","mongodb_address");
String mongodbPort =  getProperties("jdbc.porperties","mongodb_port");
String mongodb_db = getProperties("jdbc.porperties", "mongodb_db");
MongoCredential credential = MongoCredential.createCredential( mongodbUserName, mongodbDatabase, mongodbPassword.toCharArray());
ServerAddress serverAddress = new ServerAddress(mongodbAddress, Integer.parseInt(mongodbPort));
MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(credential));
MongoDatabase database = mongoClient.getDatabase(mongodb_db);

6.关于配置文件读取
public String getProperties(String filePath, String keyWord) throws IOException {
   String value = null;
   Properties prop = new Properties();
   InputStream in = null;
   if (new File("jdbc.properties").exists()) {
      in = new FileInputStream(new File("jdbc.properties"));
   } else {
      in = ReadMongoUtil.class.getResourceAsStream("/jdbc.properties");
   }
   prop.load(in);
   value = prop.getProperty(keyWord);
   return value;
}

7.打包成jar包 启动 java -jar 文件位置

随笔:

好记性不如烂笔头,随缘记录。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/759706.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号