V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kaolalicai
V2EX  ›  Java

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

  •  
  •   kaolalicai ·
    Kalengo · 2019-04-10 14:40:47 +08:00 · 3662 次点击
    这是一个创建于 1834 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    Web Service 跟 Web 接口都区分不了,你这负推广要扣工资。
    magicdu
        14
    magicdu  
       2019-04-10 19:26:26 +08:00
    同上问下 IDEA 的配色方案
    allanzhuo
        15
    allanzhuo  
       2019-04-10 19:29:32 +08:00
    我还以为是 soap
    Jrue0011
        16
    Jrue0011  
       2019-04-10 23:10:05 +08:00
    spring.io/guides 上复制全部的例子,一下子几十篇文章就完成了
    sandao
        17
    sandao  
       2019-04-11 08:51:11 +08:00
    同样看见 WebService 跑进来……还奇怪这远古的东西还有人用啊
    shawndev
        18
    shawndev  
       2019-04-11 12:12:45 +08:00
    IDE 和 IDEA 不是一个概念。集成开发环境( Integrated Development Environment,简称 IDE )而 IDEA 是 Jetbrain 开发的一款 Java 集成开发环境。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3048 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 00:18 · PVG 08:18 · LAX 17:18 · JFK 20:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.