gpt4 book ai didi

java - 启动 JVM REST 服务,我应该使用什么堆栈?

转载 作者:行者123 更新时间:2023-11-30 07:23:27 25 4
gpt4 key购买 nike

如果从头开始,只需要 JVM 和现有的中等复杂性的 MySQL 数据库,并且目标是只进行 REST,不做其他事情,我应该使用什么组件的好示例?

希望尽可能简单。

最佳答案

一些简单的选项可以是:

Spark Framework ,受 Sinatra 启发 + 使用了很好的 Java 8 功能。快速启动示例:

import static spark.Spark.*;

public class HelloWorld {
public static void main(String[] args) {
get("/hello", (req, res) -> "Hello World");
}
}

Spring Boot ,一种启动 Spring 生态系统的简单方法。快速启动:

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}

public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}

Spring 有 Spring MVC 模块用于构建 RETSful API,这是一个示例:http://spring.io/guides/gs/rest-service/

您还可以看看其他 JVM 语言,例如 Play Framework在 Scala 中或 Grails在 Groovy 中。

更新

我忘了提及Dropwizard 。它使用 JAX-RS 来实现 RESTful API,这看起来非常冗长,但它非常成熟和稳定。这是快速入门指南:http://www.dropwizard.io/0.9.2/docs/getting-started.html

关于java - 启动 JVM REST 服务,我应该使用什么堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37151594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com