kaolalicai
V2EX  ›  Java

如何使用 Spring Boot 初始一个 webService 项目

  •  
  •   kaolalicai ·
    Kalengo · Apr 10, 2019 · 4895 views
    This topic created in 2611 days ago, the information mentioned may be changed or developed.

    本文会练习使用 IDE 建立一个 mongodb 的简单 web 服务,尽量会很详细,做到初次看的也能建立成功。

    1. 使用 IDE

    Java 开发推荐使用 IDE,可以免去你很多麻烦。

    第一步,建立你的项目: File->New->Project...,选择 Spring Initializr。

    默认点击 Next-> 就好了。

    选择依赖,本项目先起一个简单的 mongodb web 服务,所以选择了 web 和 mongodb 的 dependencies,然后点击 next:

    最后一步也点击 next 就好。

    完成后可以看到此目录结构:

    2. 编写项目

    可以看到 com.example.demo 目录下会有一个 DemoApplication.java 的文件, 这个就是整个服务的入口文件。这个文件我们基本不用去碰它做任何改动。
    建立一个 web 服务,通常的步骤:第一步是建立路由,第二步是写个 controller,第三步是建立 service,第四步是 service 调用 Dao 层控制 db。

    2.1 建立路由,写个 controller

    首先直接在 com.example.demo 目录下创建个 controller 文件,java 里面 router 直接可以用注解完成,不用建立一个文件专门存 router 地址。

    // AccountController
    package com.example.demo;
    
    import java.util.concurrent.atomic.AtomicLong;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.beans.factory.annotation.Autowired;
    import java.util.List;
    
    @RestController
    @RequestMapping("/api/v1")
    public class AccountController {
        
        @RequestMapping("/account")
        public String account() {
            return "hello world";
        }
    }
    
    

    这就是一个简单的 web 服务接口。点击 IDE 的启动就可以跑起来。

    然后访问以下所写的地址就可以得到返回结果;

    2.2 建立 Dao 层

    在 com.example.demo 目录下创建 AccountRepository.java,引入 Account 类,直接继承 MongoRepository。

    // AccountRepository
    package com.example.demo;
    
    import java.util.List;
    
    import com.example.demo.Account;
    import org.springframework.data.mongodb.repository.MongoRepository;
    import org.springframework.data.rest.core.annotation.RepositoryRestResource;
    
    @RepositoryRestResource
    public interface AccountRepository extends MongoRepository<Account, String> {
    }
    

    在 com.example.demo 目录下创建 Account.java

    // Acount
    package com.example.demo;
    import org.springframework.data.annotation.Id;
    // import org.springframework.data.mongodb.core.index.CompoundIndex;
    import org.springframework.data.mongodb.core.mapping.Document;
    import org.bson.types.ObjectId;
    import java.io.Serializable;
    
    @Document(collection = "account")
    public class Account implements Serializable {
      @Id
      private ObjectId _id;
    
      private String realName;
    
      public ObjectId getId() {
        return _id;
      }
    
      public String getName() {
        return realName;
      }
    
      public String setName(String realName) {
        return this.realName = realName;
      }
    }
    

    以上就是简单的引用一个 Account Collection 的实现,

    最后还要在 application.properties 指定数据库的连接,这个文件放在 resources,一般项目的配置类参数都写在这里。

    如果从来没起过一个 mongodb 的话, 先去查查。

    spring.data.mongodb.uri=mongodb://localhost:27017/test_invest
    

    在引入了 db 的 collection 之后,controller 可以做更多的东西了。

    以下,简单写了个获取 account 这个 collection 内所有 document 的方法, 还有插入一个 account 的方法:

    // AccountController
    package com.example.demo;
    
    import java.util.concurrent.atomic.AtomicLong;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.beans.factory.annotation.Autowired;
    import java.util.List;
    
    @RestController
    @RequestMapping("/api/v1")
    public class AccountController {
        @Autowired
        AccountRepository accountRepository;
    
        @RequestMapping("/account")
        public List<Account> account() {
            return accountRepository.findAll();
        }
    
        @RequestMapping("/addAccount")
        public Account addAccount(@RequestParam String name) {
            System.out.println(name);
            Account account = new Account();
            account.setName(name);
            Account result = accountRepository.insert(account);
            System.out.println(result);
    
            return result;
        }
    }
    
    

    整个项目的目录结构是这样的:

    再次运行项目:

    1. 插入一个 document

    1. 查看表中所有的 document

    以上就已经完整的实现了一个接口服务。
    项目 demo 地址:[[https://github.com/yuchenzhen/spring-boot-demo]]

    18 replies    2019-04-11 12:12:45 +08:00
    arseBurger
        1
    arseBurger  
       Apr 10, 2019
    可以兄弟,前排顶一个
    rockyou12
        2
    rockyou12  
       Apr 10, 2019
    说 webservice 我虎躯一震,还以为会说 soap 这些上古技术😰
    accfcx
        3
    accfcx  
       Apr 10, 2019 via Android   ❤️ 2
    demo 和教程之类的都往 V2 上放!下一个 CSDN ?
    seashell84
        4
    seashell84  
       Apr 10, 2019
    老弟,web 和 WebService 不是一回事
    FrailLove
        5
    FrailLove  
       Apr 10, 2019
    搭楼问下 IDEA 的配色方案
    Alex5467
        6
    Alex5467  
       Apr 10, 2019
    看的我是莫名其妙啊
    Alex5467
        7
    Alex5467  
       Apr 10, 2019
    吓的我赶紧去百度了一下 webService 和 web 网络编程
    xy2401
        8
    xy2401  
       Apr 10, 2019
    idea 是 ide 的一种。。而且 spring 的支持 应该 只有旗舰版 才有
    br00k
        9
    br00k  
       Apr 10, 2019
    建议了解一下 jhipster,只能说真香。😂
    knva
        10
    knva  
       Apr 10, 2019
    我还以为是 soap
    uuau
        11
    uuau  
       Apr 10, 2019
    WebService ? Web Service ?
    所以楼主这个就是一个简单的 spring-boot-demo 吗?
    watermelon11
        12
    watermelon11  
       Apr 10, 2019
    看楼主勾选了 webflux,还期待了下。。。。 手动狗头
    passerbytiny
        13
    passerbytiny  
       Apr 10, 2019
    在非推广区变着法搞推广,你是找举报吗?

    Web Service 跟 Web 接口都区分不了,你这负推广要扣工资。
    magicdu
        14
    magicdu  
       Apr 10, 2019
    同上问下 IDEA 的配色方案
    allanzhuo
        15
    allanzhuo  
       Apr 10, 2019
    我还以为是 soap
    Jrue0011
        16
    Jrue0011  
       Apr 10, 2019
    spring.io/guides 上复制全部的例子,一下子几十篇文章就完成了
    sandao
        17
    sandao  
       Apr 11, 2019
    同样看见 WebService 跑进来……还奇怪这远古的东西还有人用啊
    shawndev
        18
    shawndev  
       Apr 11, 2019
    IDE 和 IDEA 不是一个概念。集成开发环境( Integrated Development Environment,简称 IDE )而 IDEA 是 Jetbrain 开发的一款 Java 集成开发环境。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5589 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 91ms · UTC 08:26 · PVG 16:26 · LAX 01:26 · JFK 04:26
    ♥ Do have faith in what you're doing.