今天开始学习Spring Boot,后面的文章会结合两者区别一边学习一边理解用法上的区别
Java环境配置就不说明了
Java:
创建一个类 命名为 HomeController
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HomeController { @RequestMapping("/helloworld") public String Index() { return "Hello World"; } }
.NetCore
using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; namespace ExpressServices.Controllers { public class HomeController : Controller { [Route("~/helloworld")] public IActionResult Index() { return Content("Hello World"); } } }
路由区别
路由:@RequestMapping("/helloworld") vs [Route("~/helloworld")]
包引用区别
import org.springframework.web.bind.annotation.RestController
using Microsoft.AspNetCore.Mvc;
主程序入口
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseUrls("http://localhost:20002") .UseStartup<Startup>(); }
都有很多相似之处,思想上没什么区别,所以 一个Hello World 很快就搞定了~
运行下Spring Boot 项目