java entryset,sping boot demo解釋

 2023-11-19 阅读 55 评论 0

摘要:? ? ?? ? ? ? Spring boot設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置 ? ? 用spring的方法來構建RESTful Web服務,HTTP請求是由controller處理,這些組件可以由@ restcontroller注解 ? ? ? @RestControlle

? ? ??

? ? ? Spring boot設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置

? ? 用spring的方法來構建RESTful Web服務,HTTP請求是由controller處理,這些組件可以由@ restcontroller注解

? ? ?

@RestController
public class GreetingController {private static final String template = "Hello, %s!";private final AtomicLong counter = new AtomicLong();@RequestMapping("/greeting")public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {return new Greeting(counter.incrementAndGet(),String.format(template, name));}
}
? ? 注解@requestMapping 默認支持post,get,put,可以使用@RequestMapping(method=GET)

? ?@ RequestParam值在greeting()方法名稱參數,這個查詢字符串參數不是必需的;如果它在請求缺席,默認值會被使用。這個RESTful Web服務 ? ? ? ? ?controller簡單地返回一個請求的對象,然后對象被直接寫入HTTP響應JSON格式中。此代碼使用spring4的新”restcontroller注釋,這注解標志作為一 ? ?個控制器,每一個方法返回一個對象而不是一個view,這個注解包括了@ responsebody,@controller。請求的對象必須被轉換成JSON,感謝spring ?? 的HTTP消息轉換器支持,你不需要手動做這種轉換。因為Jakeson 2在classp中,spring的mappingjackson2httpmessageconverter自動選擇轉換到 ? ? JSON實例。

傳統上市打包成war部署到服務器中,下面演示創建一個獨立的application。把一切打包到一個可執行jar文件中,由一個Java main()方法驅動。并 且,也可以使用spring支持的embedding the Tomcat servlet container運行。

@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
@SpringBootApplication 注解包括

? ?@Configuration ?the application context 定義beans

? ?@EnableAutoConfiguration?告訴spring啟動開始添加基于路徑的配置的beans,其他beans,及各種設置。

? ?你通常會為Spring MVC應用添加@EnableWebMvc,但spring boot自動添加,當它看到Springwebmvc在classpath。這個標志中的應用作為一個Web應用程序,并激活如設置DispatcherServlet。?

? ? ??@ComponentScan告訴spring尋找其他組件,配置,和services(在hello包中),讓它找到hellocontroller?

? ? ??main()使用spring boot的springapplication run()方法來啟動應用程序。你有沒有注意到沒有一條XML?沒有web.xml文件。這個web應用程序是100%純的java。


?http://localhost:8080/greeting

{"id":1,"content":"Hello, World!"}
http://localhost:8080/greeting?name=User
{"id":2,"content":"Hello, User!"}

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.springframework</groupId><artifactId>gs-rest-service</artifactId><version>0.1.0</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.2.5.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><properties><java.version>1.7</java.version></properties><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-releases</id><url>https://repo.spring.io/libs-release</url></repository></repositories><pluginRepositories><pluginRepository><id>spring-releases</id><url>https://repo.spring.io/libs-release</url></pluginRepository></pluginRepositories></project>

? ? ??





版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/183421.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息