当前位置: 移动技术网 > IT编程>开发语言>Java > 2018-08-14 中文代码之Spring Boot实现简单REST服务

2018-08-14 中文代码之Spring Boot实现简单REST服务

2019年03月09日  | 移动技术网IT编程  | 我要评论

最终目标详见: 参考msdn,试搞.net类库标识符的翻译版 · issue #54 · program-in-chinese/overview

此文仅为技术探索+原型搭建的第一小步.

源码库:

主要部分源码如下:
应用.java

@springbootapplication
public class 应用 {

  public static void main(string[] 参数) {
    springapplication.run(应用.class, 参数);
  }

}

词典控制器.java

@restcontroller
public class 词典控制器 {

  private static final hashmap<string, string> 英中词典 = new hashmap<>();

  static {
    英中词典.put("list", "列表");
  }
  private final atomiclong 计数器 = new atomiclong();

  @getmapping("/")
  @responsebody
  public 词条 取词条(@requestparam(name = "term", required = false, defaultvalue = "") string 英文术语) {
    if (英中词典.containskey(英文术语)) {
      return new 词条(计数器.incrementandget(), 英中词典.get(英文术语));
    }
    return null;
  }
}

词条.java

public class 词条 {
  // todo: 仅为演示用
  private final long id;
  private final string 中文术语;

  public 词条(long id, string 中文术语) {
    this.id = id;
    this.中文术语 = 中文术语;
  }

  public long getid() {
    return id;
  }

  public string get中文术语() {
    return 中文术语;
  }
}

在演示服务器上构建并从jar包启动服务:

$ mvn package && java -jar target/programming-term-dictionary-0.1.0.jar --server.port=8090

在本地(客户端)访问, 或直接在浏览器访问http://74.91.17.250:8090/?term=list:

$ curl 74.91.17.250:8090?term=list
{"id":3,"中文术语":"列表"}

参考文档: building a restful web service with spring boot actuator

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网