Java Spring Boot VS .NetCore (一)来一个简单的 Hello World
系列文章
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World
Java Spring Boot VS .NetCore (二)实现一个过滤器 Filter
Java Spring Boot VS .NetCore (三)Ioc 容器处理
Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore
Java Spring Boot VS .NetCore (五)MyBatis vs EFCore
Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml
Java Spring Boot VS .NetCore (七) 配置文件
Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute
Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security
Java Spring Boot VS .NetCore (十) Java Interceptor vs .NetCore Interceptor
Java Spring Boot VS .NetCore (十一)自定义标签 Java Tag Freemarker VS .NetCore Tag TagHelper
今天开始学习 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")</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String Index() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> "Hello World"<span style="color: rgba(0, 0, 0, 1)">; }
}
.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 {</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { SpringApplication.run(DemoApplication.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">, args); }
}
public class Program { public static void Main(string[] args) {CreateWebHostBuilder(args).Build().Run(); }</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> IWebHostBuilder CreateWebHostBuilder(<span style="color: rgba(0, 0, 255, 1)">string</span>[] args) =><span style="color: rgba(0, 0, 0, 1)"> WebHost.CreateDefaultBuilder(args) .UseUrls(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">http://localhost:20002</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">) .UseStartup</span><Startup><span style="color: rgba(0, 0, 0, 1)">(); }</span></pre>
都有很多相似之处,思想上没什么区别,所以 一个 Hello World 很快就搞定了 ~
运行下 Spring Boot 项目