7款Java 微服务框架
以下是针对如何选择合适的Java微服务框架的示例代码。这些代码示例展示了如何在Java中使用不同的微服务框架来创建RESTful API。
- Spring Boot:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class SpringBootApplicationExample {
@GetMapping("/")
public String hello() {
return "Hello, Spring Boot!";
}
public static void main(String[] args) {
SpringApplication.run(SpringBootApplicationExample.class, args);
}
}
- Spring Cloud:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class SpringCloudApplicationExample {
@GetMapping("/")
public String hello() {
return "Hello, Spring Cloud!";
}
public static void main(String[] args) {
SpringApplication.run(SpringCloudApplicationExample.class, args);
}
}
- Dropwizard:
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import javax.servlet.ServletException;
import java.util.EnumSet;
public class DropwizardApplicationExample extends Application<DropwizardConfiguration> {
@Override
public void initialize(Bootstrap<DropwizardConfiguration> bootstrap) {
// nothing to do yet
}
@Override
public void run(DropwizardConfiguration configuration, Environment environment) throws ServletException {
environment.jersey().register(new HelloWorldResource());
}
public static void main(String[] args) throws Exception {
new DropwizardApplicationExample().run(args);
}
}
class HelloWorldResource {
@GET
@Path("/hello-world")
@Produces(MediaType.TEXT_PLAIN)
public String helloWorld() {
return "Hello, Dropwizard!";
}
}
`
评论已关闭