springboot+mongodb+swagger

基于springboot+mongodb+swagger搭建的简单项目

1. 项目的基本结构**
这里写图片描述

2. 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.westlife</groupId>
    <artifactId>spring-boot-mongodb</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0</version>
    <name>spring-boot-mongodb Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jdk.version>1.7</jdk.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <!-- 增加mongodb支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.16</version>
        </dependency>

        <!-- swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>


        <!-- Junit 单元测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <finalName>spring-boot-mongodb</finalName>

        <plugins>
            <!-- 如果要maven打包成jar,必须要有下面的maven插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

    </build>
</project>

3. springboot的application.yml配置文件
springboot除了application.properties这种配置文件,还可以使用application.yml。下面附上application.yml的配置信息。具体两者没有什么区别

server:
    port: 8083
#    context-path: /mongodb

spring: 
    data: 
        mongodb: 
             uri: mongodb://localhost:27017/test
            #uri: mongodb://root:123456@127.0.0.1:27017/test

端口号:8083 默认是8080。可以根据自己要求修改

4. SwaggerConfig.java的配置信息

/**
 * 
 */
package com.westlife;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 
 * @author  westlife
 * @date 创建时间:2017年12月20日 上午11:23:27 
 * @version 1.0 
 * @parameter
 *
 */
@EnableSwagger2
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() // 为当前包路径
                .apis(RequestHandlerSelectors
                .basePackage("com.westlife"))
                .paths(PathSelectors.any())
                .build();
    }

    // 构建 api文档的详细信息函数
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder() // 页面标题
                .title("Spring Boot 测试使用 Swagger2 构建RESTful API")
                .termsOfServiceUrl("http://localhost/")          
                .contact("westlife") // 创建人
                .version("1.0") // 版本号          
                .description("部署信息:172.0.0.1:8083")//描述
                .build();
    }

}
  1. UserRepository.java
package com.westlife.dao;

import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;

import com.westlife.domain.User;

/**
 * 
 * @author  westlife
 * @date 创建时间:2017年12月20日 上午9:17:07 
 * @version 1.0 
 * @parameter
 *
 */
public interface UserRepository extends MongoRepository<User, String> {

    List<User> findByName(String name);
}
  1. UserController.java
package com.westlife.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.westlife.common.RequestDto;
import com.westlife.domain.User;
import com.westlife.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
/**
 * 
 * @author  westlife
 * @date 创建时间:2017年12月20日 上午9:16:38 
 * @version 1.0 
 * @parameter
 *
 */
@Api(value = "测试Mongodb",description="简单的API")
@RestController
@EnableScheduling//定时任务的注解
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MongoTemplate mongoTemplate;

    @ApiOperation(value = "添加用户") 
    @RequestMapping(value="/save", method = RequestMethod.POST)
    public User save(@RequestBody(required = true) User user) {
        mongoTemplate.save(user);
        return user;
    }

    @ApiOperation(value = "查询所有用户") 
    @RequestMapping(value="/find", method = RequestMethod.POST)
    public List<User> find() {
        List<User> userList = mongoTemplate.findAll(User.class);
        return userList;
    }

    @ApiOperation(value = "按名称查询用户查询用户") 
    @RequestMapping(value="/findByName", method = RequestMethod.POST)
    public List<User> findByName(@RequestBody(required = true) RequestDto requestDto) {
        List<User> userList = userService.findByName(requestDto.getName());
        return userList;
    }
 // 每2分钟执行一次
        @Scheduled(cron = "0 02 * * * ? ")
        public void timer() {
            mongoTemplate.dropCollection(User.class);;//删除集合,可传实体类,也可以传名称
        }
}
  1. 实体类User
package com.westlife.domain;

import java.io.Serializable;

import org.hibernate.validator.constraints.NotEmpty;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 
 * @author  westlife
 * @date 创建时间:2017年12月20日 上午9:17:21 
 * @version 1.0 
 * @parameter
 *
 */
@ApiModel(value = "用户信息", description = "用户信息")
public class User implements Serializable{
    private static final long serialVersionUID = 1492973623389075081L;

    @NotEmpty(message="id不能为空!!!")
    private String id;
    @NotEmpty(message="name不能为空!!!")
    private String name;
    @NotEmpty(message="age不能为空!!!")
    private String age;

    @ApiModelProperty("唯一标识")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    @ApiModelProperty("姓名")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @ApiModelProperty("年龄")
    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
    } 
}

8. 访问swagger
这里写图片描述
这里写图片描述

9. 可以查看mongodb****
这里写图片描述
添加成功

项目源码点击下面的链接

git地址源码点击处

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值