@RestController @RequestMapping("/api") public class HelloController { @GetMapping("/hello") public String helloWorld() { return "Hello, World!"; } } @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } @Path("/api") public class HelloResource { @GET @Path("/hello") public String helloWorld() { return "Hello, World!"; } } public class MyApplication extends ResourceConfig { public MyApplication() { packages("com.example"); } } public class Main { public static void main(String[] args) throws IOException { URI baseUri = UriBuilder.fromUri("http://localhost/").port(8080).build(); ResourceConfig config = new MyApplication(); HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, config); server.start(); System.out.println("Server started on " + baseUri); System.in.read(); server.shutdown(); } } public class HelloWorldResource extends ServerResource { @Get public Representation helloWorld() { return new StringRepresentation("Hello, World!", MediaType.TEXT_PLAIN); } } public class MyApplication extends Application { @Override public synchronized Restlet createInboundRoot() { Router router = new Router(getContext()); router.attach("/api/hello", HelloWorldResource.class); return router; } } public class Main { public static void main(String[] args) throws Exception { Component component = new Component(); component.getServers().add(Protocol.HTTP, 8080); component.getDefaultHost().attach(new MyApplication()); System.out.println("Server started on http://localhost:8080"); component.start(); } }


上一篇:
下一篇:
切换中文