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;

    /**
     * 测试 http://localhost:8082/test/getUser
     * @return
     */
    @RequestMapping("/getUser")
    public String welcome(){
        return "Crud Spring Boot Project ! ";
    }

    /**
     * http://localhost:8082/test/testlist
     * @return
     * @throws IOException
     */
    @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 文件位置

随笔:

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

你可能感兴趣的:(springboot,cmd,笔记+好书存档,spring,boot,maven,intellij-idea)