




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
目 錄寫在前面 3—介紹 4二工具 4Java 4Maven 4Subversion 4三源代碼 4源代碼 5提交 5庫結(jié)構(gòu) 5分支結(jié)構(gòu) 6快速入門 6取出源代碼 6由Maven構(gòu)建 7由Maven生成Eclipse工程文件 9導入模塊進Eclipse 9在Eclipse中運行GeoServer 12訪問GeoServer首頁 13Maven指南 13安裝Maven 13運行Maven 13構(gòu)建 13跳過測試 14離線構(gòu)建 14構(gòu)建擴展 14Profiles 15Eclipse 15構(gòu)建Web模塊 15使用Jetty運行web模塊 16Eclipse指南 16導入模塊 17運行和調(diào)試 17設置數(shù)據(jù)目錄 17更改Jetty默認端口 18配置Jetty的JNDI資源 19Eclipse首選項 20代碼格式化 21代碼模板 22文本編輯器 22編譯器 23OWS服務 24OWS服務概覽 24實現(xiàn)一個簡單的OWS服務 24建立 25創(chuàng)建插件 試一試 30REST服務 34簡介 34RESTful服務實現(xiàn) 35基于Maps的Restful服務實現(xiàn) 42基于反射的Restful服務實現(xiàn) 48中的Wicket開發(fā) 54用戶接口 60簡介 60實現(xiàn)一個WicketUI擴展 61\h寫在前面\h\h\hJava\hMaven\hSubversion\h三源代碼\h源代碼\h提交\h庫結(jié)構(gòu)\h分支結(jié)構(gòu)\h四快速入門\h取出源代碼\h由Maven構(gòu)建\h由Maven生成Eclipse工程文件\h導入模塊進Eclipse\h在Eclipse中運行GeoServer\h訪問GeoServer首頁\hMaven指南\hEclipse指南\h七編程指南\h八釋放指南\h九釋放測試檢查列表\h十CITE測試指南\h十一政策與步驟寫在前面\hOSGeo的軟件和應用很感興趣,希望致力于這些軟件的普及與應用,《GeoServerWebMap/GISGoogleGeoServerMapServer圖1GeoServer、MapServer趨勢分析GeoServer(GeoServerMapServer去編輯某些文件。圖2GeoServer搜索量前5名國家(見Google趨勢)2.0.2我交流?!榻B空。二工具在GeoServer開發(fā)者環(huán)境搭建以前,需要安裝以下工具:JavaGeoServerJDK1.5\hSunMicrosystems獲取。注:除了可以使用Sun提供的JDK以外,也可以使用其它的,但這里我們推薦使用Sun的。MavenGeoserver\hMaven2.1.0,\hApache2.0.8SubversionGeoserversubversionsubversion客戶端可獲取。更多細節(jié)請訪問:\h/getting.html。三源代碼源代碼GeoServer源代碼存放在:\h/geoserver。使用以下方式取出開發(fā)版/trunk版本\hsvnco/geoserver/trunkgeoserver使用以下方式取出穩(wěn)定版/stable版本:svncosvnco\h/geoserver/branches/1.7.xgeoserver或者很昂貴,這種情況下,用戶可以通過以下方式僅獲取源代碼:svncosvnco\h/geoserver/trunk/src提交通過以下步驟提交到代碼庫:subversion\h配置文件,請看后面?zhèn)渥?。注冊獲取提交權(quán)限。將庫切換到”https”協(xié)議,例如:[rootofcheckout]%svnswitch/geoserver/trunk[rootofcheckout]%svnswitch/geoserver/trunk庫結(jié)構(gòu)\h\h/geoserver/branches/spike/tags/trunk/branches包含以前所有穩(wěn)定的開發(fā)分支,1.6.x,1.7.x,etc…spike包含實驗版本和原型tags包含以前所有釋放版本trunk為當前開發(fā)分支。分支結(jié)構(gòu)每個開發(fā)分支都有如下所示的結(jié)構(gòu):\h\h/geoserver/doc/src/data/doc包含用戶和開發(fā)人員向?qū)У脑次募rcGeoserverJava源碼dataGeoServer的數(shù)據(jù)目錄快速入門GeoServer的工具(內(nèi)容見第二章)。GeoServer\hMaven\hEclipse。取出源代碼由Maven構(gòu)建由Maven生成Eclipse工程文件導入模塊進Eclipse在Eclipse里運行GeoServer訪問GeoServer首頁取出源代碼subversiontrunk更新的版本的穩(wěn)定分支。要么是:svnco/geoserver/trunkgeoserver-trunksvnco/geoserver/trunkgeoserver-trunk或者(穩(wěn)定的1.7.x分支)svncosvnco\h/geoserver/branches/1.7.xgeoserver-1.7.xgeoserver名字取得更詳細一點為好。由Maven構(gòu)建修改目錄到源樹的根,執(zhí)行以下maven構(gòu)建命令:cdgeoserver/srccdgeoserver/srcmvncleaninstall成功構(gòu)建后會產(chǎn)生類似于以下的輸出:[INFO][INFO][INFO][INFO][INFO]ReactorSummary:[INFO][INFO]GeoServer SUCCESS[10.271s][INFO]GeoServerMavenPlugins SUCCESS[0.865s][INFO]ConfigurationDeploymentPlugIn SUCCESS[3.820s][INFO]GeoServerMavenArchetypes SUCCESS[0.054s][INFO]GeoServerWFSOutputFormatArchetype SUCCESS[0.390s][INFO]CorePlatformModule SUCCESS[5.270s][INFO]DataModule SUCCESS[4.521s][INFO]OpenWebServiceModule SUCCESS[2.730s][INFO]MainModule SUCCESS[10.077s][INFO]WebCoverageServiceModule SUCCESS[3.785s][INFO]WebCoverageService1.1.1Module SUCCESS[5.254s][INFO]ValidationModule SUCCESS[1.131s][INFO]WebFeatureServiceModule SUCCESS[6.695s][INFO]WebFeatureServiceModule SUCCESS[1.197s][INFO]WebMapServiceModule SUCCESS[8.519s][INFO]GeoserverRESTSupportCode SUCCESS[3.366s][INFO]GeoWebCache(GWC)Module SUCCESS[0.255s][INFO]Web[INFO]WebApplicationModule SUCCESS[27.386s][INFO]CommunitySpace SUCCESS[0.312s][INFO]GeoServerExtensions SUCCESS[INFO][INFO][INFO]BUILDSUCCESSFUL[INFO]由Maven生成Eclipse工程文件生成Eclipse使用的.project文件和.classpath文件:mvneclipse:eclipsemvneclipse:eclipse導入模塊進EclipseEclipse集成開發(fā)環(huán)境EclipsePreferences依次點擊進入:Java,BuildPath,ClasspathVariablesNew創(chuàng)建類路徑變量名稱”M2_REPO”,并且設置它的值為本地MavenOK。OKEclipse首選項。PackageExplorerImport…ExistingProjectsintoWorkspace,單擊下一步:geoserver/src目錄Finish在Eclipse中運行GeoServerPackageExplorerweb-app模塊(1.7.xweb)org.geoserver.web包(1.7.xorg.vfny.geoserver.jetty)StartRunas,JavaApplication訪問GeoServer首頁幾秒后,就可通過http://localhost:8080/geoserverGeoServeradmingeoserverMaven指南本章是使用Maven構(gòu)建GeoServer的參考。安裝Maven見工具(第二章)運行MavenMavenmaven運行,或者某個特模塊。注:在準備由源樹的根運行maven命令時,請記住要將取出的根目錄變換到src目錄。在由源樹的根運行一個命令時,或者從包含其它模塊的目錄時,命令將會為所有模塊運行。當從單個模塊運行命令時,它只為這個模塊運行。構(gòu)建GeoServer最常用的命令是安裝命令:mvncleaninstallmvncleaninstall雖然clean命令不是必須的,但我們還是推薦用上。上面的命令做了以下的事:編譯源代碼運行單元測試Maven庫跳過測試-DskipTests元測試而不會運行測試。mvn-DskipTestscleaninstallmvn-DskipTestscleaninstall離線構(gòu)建Maven有可能會有問題。MavenGeoServerGeoToolsMavenGeoTools得不必要了。這時,可以使用“離線”方式運行Maven來改進:mvn-ocleaninstallmvn-ocleaninstall在離線模式中,Maven不會下載任何外部依賴,也不會更新任何快照依賴。構(gòu)建擴展Profilerestconfigmvncleaninstall-Prestconfigmvncleaninstall-Prestconfig可以同時添加多個擴展同時使用:mvncleaninstall-Prestconfig,oraclemvncleaninstall-Prestconfig,oracleallExtensionsProfile可以添加所有擴展:mvncleaninstall-PallExtensionsProfilesEclipseMaven的Eclipse插件用于為模塊生成eclipse工程:mvneclipse:eclipsemvneclipse:eclipse經(jīng)過此操作后,這些模塊就可以導入Eclipse工作區(qū)。該插件有個很有用的功能就是可下載第三方依賴的相關源代碼。這是通過添加downloadSources標志來做的:mvn-DdownloadSourceseclipse:eclipsemvn-DdownloadSourceseclipse:eclipsedownloadSourcesGeoServer構(gòu)建Web模塊webminimalconfigIdconfigDirectorymvncleaninstall-DconfigId=releasemvncleaninstall-DconfigId=release-DconfigDirectory=/home/jdeolive/geoserver_1.7.x/dataGeoServerreleasewebconfigIdconfigDirectorywebmvncleaninstall-DconfigId=release-DconfigDirectory=../../../datamvncleaninstall-DconfigId=release-DconfigDirectory=../../../data上面的命令與之前有同樣的效果,盡管是引用了相對于web模塊的路徑。該路徑,../../../data,GeoServer(checkout)可用。使用Jetty運行web模塊Maven的jetty插件可用于運行嵌入了jetty容器的基于web的模塊:cdgeoserver_1.7.x/src/webcdgeoserver_1.7.x/src/webmvnjetty:run注:該命令必須從web模塊運行,如果從其它模塊運行就會失敗。GeoServerGEOSERVER_DATA_DIRmvn-DGEOSERVER_DATA_DIR=/path/to/datadirjetty:runmvn-DGEOSERVER_DATA_DIR=/path/to/datadirjetty:runEclipse指南本章是使用Eclipse開發(fā)GeoServer的參考。導入模塊運行和調(diào)試設置數(shù)據(jù)目錄更改Jetty默認端口配置Jetty中JNDI資源Eclipse首選項代碼格式化代碼模塊文本編輯器編譯器導入模塊參見\hMaven指南中的Eclipse部分。運行和調(diào)試運行和調(diào)試web-app模塊中org.geoserver.web.Start類。具體步驟見\h快速入門。設置數(shù)據(jù)目錄web-appminimal要修改,執(zhí)行以下步驟:Eclipse(DebugConfigurations)StartArgument-DGEOSERVER_DATA_DIR目錄為絕對路徑。更改Jetty默認端口如果未設置,Jetty默認會使用8080端口。修改請按以下方式:StartArgument面板。填寫-Djetty.port參數(shù),設置它到期望的端口配置Jetty的JNDI資源JNDIjetty.config.fileJettyStartArgumentVMargumentweb-app-Djetty.config.file=../../../../../settings/jetty.xml-Djetty.config.file=../../../../../settings/jetty.xmlJettyJNDIOracle據(jù)庫的連接池。jdbc/demojdbc/demooracle.jdbc.driver.OracleDriverjdbc:oracle:thin:@:1521:demodbclaudiuss3cr3t201001000030000030000020true100trueSELECTSYSDATEFROMDUALJettyGeoServerWEB-INF/web.xmlreference-refGeoServerweb-appJettyJNDIJettyTomcatGeoServer\hTomcat\hJNDI章節(jié)。Eclipse首選項代碼格式化下載文件:\h/geotools/trunk/build/eclipse/formatter.xmlJava,CodeStyle,F(xiàn)ormatterImport…1formatter.xml文件。Apply代碼模板下載文件:\h/geotools/trunk/build/eclipse/codetemplates.xmlJava,CodeStyle,Formatter,Important…1formatter.xml文件。Apply文本編輯器General,Editors,TextEditorsInsertSpacefortabsShowprintmarginPrintmargincolumn100ShowlinenumbersShowwhitespacecharacters(可選)注:顯示空白字符可幫助我們發(fā)現(xiàn)無意中提交不必要的空格。Apply編譯器Java,Compiler,BuildingOutputfolder,并將.svn/Filteredresources列表中。ApplyOWS服務本節(jié)介紹GeoServer中OWS服務(原文為RESTful服務,可能為筆誤)如何工作。OWS服務概覽TBA實現(xiàn)一個簡單的OWS服務GeoServerGeoServerOWS需要提供一個能廣告叫做“sayHello”的單一操作的功能(capabilities)sayHello操作的結(jié)果是一個簡單的字符串:“HelloWorld”。內(nèi)容:實現(xiàn)一個簡單的OWS服務建立創(chuàng)建插件試一試建立maven工程。該工程我們稱為“hello”。hello的新目錄。hellopom.xml文件。<?xmlversion="1.0"encoding="ISO-8859-1"?><?xmlversion="1.0"encoding="ISO-8859-1"?>\h<projectxmlns="/POM/4.0.0"\hxmlns:xsi="/2001/XMLSchema-instance"\hxsi:schemaLocation="/POM/4.0.0\h/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><!--setparentpomtocommunitypom--><parent><groupId>org.geoserver</groupId><artifactId>community</artifactId><version>2.0.1</version></parent><groupId>org.geoserver</groupId><artifactId>hello</artifactId><packaging>jar</packaging><version>1.0</version><name>HelloWorldServiceModule</name><!--declaredepenencyongeoservermain--><dependencies><dependency><groupId>org.geoserver</groupId><artifactId>main</artifactId><version>2.0.1</version></dependency></dependencies><repositories><repository><id>opengeo</id><name>opengeo</name>\h<url></url></repository></repositories></project></project>helloJava源文件目錄。hello/hello/+pom.xml+src/+main/+java/創(chuàng)建插件插件是一系列由springbeans實現(xiàn)的擴展。實例中擴展興趣點是一個HelloWorld的POJO。HelloWorld的類:importjava.io.IOException;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassHelloWorld{publicHelloWorld(){//Donothing}publicpublicvoidsayHello(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.getOutputStream().write("HelloWorld".getBytes());}}該服務相對比較簡單。它提供了一個一個名為sayHello的方法,帶有HttpServletRequest,HttpServletResponse參數(shù)。該函數(shù)的參數(shù)列表會由org.geoserver.ows.Dispatcher自動發(fā)現(xiàn)。applicationContext.xmlbean。<?xmlversion="1.0"encoding="UTF-8"?><?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"\h"/dtd/spring-\hbeans.dtd"><beans><!--SpringwillreferencetheinstanceoftheHelloWorldclassbytheidname"helloService"--><beanid="helloService"class="HelloWorld"></bean><!--ThiscreatesaServicedescriptor,whichallowsorg.geoserver.ows.Dispatchertolocateit.--><beanid="helloService-1.0.0"class="org.geoserver.platform.Service"><!--usedtoreferencetheserviceintheURL--><constructor-argindex="0"value="hello"/><!--ouractualservicePOJOdefinedpreviously--><constructor-argindex="1"ref="helloService"/><!--aversionnumberforthisservice--><constructor-argindex="2"value="1.0.0"/><!--alistoffunctionsforthisservice--><constructor-argindex="3"><list><value>sayHello</value></list></constructor-arg></bean></beans>到此,hello工程內(nèi)容應該是看上去像下面這樣了:hello/hello/+pom.xml+pom.xml+src/+main/+java/+HelloWorld.java+applicationContext.xml試一試1. hello模塊。[hello]%mvninstall[hello]%mvninstall[hello]%mvninstall[INFO]Scanningforprojects...[INFO][INFO]BuildingHelloWorldServiceModule[INFO]task-segment:[install][INFO][INFO][resources:resources][INFO]Usingdefaultencodingtocopyfilteredresources.[INFO][compiler:compile][INFO]Compiling1sourcefileto/home/ak/geoserver/community/hello/target/classes[INFO][resources:testResources][INFO]Usingdefaultencodingtocopyfilteredresources.[INFO][compiler:testCompile][INFO]Nosourcestocompile[INFO][surefire:test][INFO]Noteststorun.[INFO][jar:jar][INFO]Buildingjar:/home/ak/geoserver/community/hello/target/hello-1.0.jar[INFO][jar:test-jar{execution:default}][WARNING]JARwillbeempty-nocontentwasmarkedforinclusion![INFO]Buildingjar:/home/ak/geoserver/community/hello/target/hello-1.0-tests.jar[INFO][install:install][INFO]Installing/home/ak/geoserver/community/hello/target/hello-1.0.jarto/home/ak/.m2/repository/org/geoserver/hello/1.0/hello-1.0.jar[INFO]Installing/home/ak/geoserver/community/hello/target/hello-1.0-tests.jarto/home/ak/.m2/repository/org/geoserver/hello/1.0/hello-1.0-tests.jar[INFO] [INFO]BUILDSUCCESSFUL[INFO] [INFO]Totaltime:6seconds[INFO]Finishedat:FriSep2114:52:31EDT2007[INFO]FinalMemory:27M/178M[INFO]Finishedat:FriSep2114:52:31EDT2007[INFO]FinalMemory:27M/178M[INFO] target/hello-1.0.jarGeoServerWEB-INF/lib目錄。GeoServer。訪問:http:<host>/geoserver/ows?request=sayHello&service=hello&version=1.0.0請求request服務serviceapplicationContext.xmlService描述符的名稱版本versionapplicationContext.xml文件中Service描述符的版本可選方法1.與web模塊一起構(gòu)建另外一個可選方法是在新的插件工程上從web模塊申明一個依賴。hello模塊。web/pom.xml,添加如下依賴:<dependency><dependency><groupId>org.geoserver</groupId><artifactId>hello</artifactId><version>1.0</version></dependency>web模塊[web]mvninstalljetty:run[web]mvninstalljetty:run訪問:http://localhost:8080/geoserver/ows?request=sayHello&service=hello&version=1.0.0可選方法2:由GeoServer源中運行作為嘗試插件開發(fā)的一種可選方法:hello模塊。web模塊web模塊??截?lt;hellomodule>/target/hello-1.0.jar文件到<webmodule>/target/geoserver/WEB-INF/lib[/dev/geoserver/web]%cp~/hello/target/hello-1.0.jartarget/geoserver/WEB-INF/lib5.使用Jetty運行打包的war。[/dev/geoserver/web]%mvnjetty6:run-exploded[/dev/geoserver/web]%cp~/hello/target/hello-1.0.jartarget/geoserver/WEB-INF/lib5.使用Jetty運行打包的war。[/dev/geoserver/web]%mvnjetty6:run-exploded6.訪問:6.訪問:http://localhost:8080/geoserver/ows?request=sayHello&service=hello&version=1.0.0REST服務本節(jié)是GeoServer中運行RESTful服務的的預覽。簡介GeoServer\hRestletRESTRestletJava編寫的輕量restservlet的應用程序Rest派發(fā)GeoServer/restrestfulrest派發(fā)器來處理。派發(fā)器的工作職責是請每個請求導向到相關的處理終端上。該restlet。Restlet由spring環(huán)境加載,因此它們是可插拔的。RestletsRestletorg.restlet.Restlet類相對應。我們可以擴展該類以實現(xiàn)一個服務端點(服務點)finder的子類。Finders和資源RestfulFinderorg.restlet.resource.Resource。表達Representations\h?Repreentation,通常是指特殊狀態(tài)的狀態(tài)或者資源編碼的格式。例如,當某個特殊資源請求進來時,該資源的表達就返回給了客戶端。RESTful服務實現(xiàn)GeoServerrestfulhelloworldGET請求返回一個“HelloWorld”的文本。預置條件在開始之前,GeoServer必須在本地系統(tǒng)上構(gòu)建成功。具體細節(jié)見\h源代碼和\h快速入門兩節(jié)。創(chuàng)建新模塊hello_rest的新模塊。pom.xml文件到新模塊根部:\h\h<projectxmlns="/POM/4.0.0"\hxmlns:xsi="/2001/XMLSchema-instance"\hxsi:schemaLocation="/POM/4.0.0\h/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.geoserver</groupId><artifactId>hello_rest</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version><name>hello_rest</name><dependencies><dependency><groupId>org.geoserver</groupId><artifactId>rest</artifactId><version>2.0-SNAPSHOT</version></dependency><dependency><groupId>org.geoserver</groupId><artifactId>main</artifactId><version>2.0-SNAPSHOT</version><classifier>tests</classifier><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>com.mockrunner</groupId><artifactId>mockrunner</artifactId><version>0.3.1</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.5</source><target>1.5</target></configuration></plugin></plugins></build></project></project>1. src/main/java目錄[hello_rest]%mkdir-psrc/main/java[hello_rest]%mkdir-psrc/main/java創(chuàng)建資源類 org.geoserver.rest.AbstractResource類。在org.geoserver.hellorest包內(nèi)創(chuàng)建一個名為HelloResourceAbstractResource擴展。packageorg.geoserver.hellorest;packageorg.geoserver.hellorest;importjava.util.List;importorg.geoserver.rest.AbstractResource;importorg.geoserver.rest.format.DataFormat;importorg.restlet.data.Request;importorg.restlet.data.Response;publicclassHelloResourceextendsAbstractResource@OverrideprotectedList<DataFormat>createSupportedFormats(Requestrequest,response){returnnull;returnnull;}}createSupportedFormats()。該方法的目的是創(chuàng)建一個由擴展.txtWorld”的文本。importjava.util.ArrayList;importjava.util.ArrayList;importorg.geoserver.rest.format.StringFormat;...@OverrideprotectedList<DataFormat>createSupportedFormats(Requestrequest,Responseresponse){List<DataFormat>formats=newArrayList();formats.add(newStringFormat(MediaType.TEXT_PLAIN));returnformats;}2. handleGet()GET請求拿到時調(diào)用。@Override@OverridepublicvoidhandleGet(){//gettheappropriateformatDataFormatformat=getFormatGet();//gettheappropriateformatDataFormatformat=getFormatGet();//transformthestring"HelloWorld"totheappropriateresponsegetResponse().setEntity(format.toRepresentation("HelloWorld"));}getFormatGet()方法,該方法的目的是確定客戶端請求的擴展,并為它找StringFormat創(chuàng)建了。創(chuàng)建應用程序環(huán)境GeoServer上節(jié)中所創(chuàng)建的資源。在hello_rest模塊根部創(chuàng)建src/main/resources目錄。[hello_rest]%mkdirsrc/main/resources[hello_rest]%mkdirsrc/main/resourceshello_restsrc/main/resources目錄中添加如下的applicationContext.xml文件。<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"\h"/dtd/spring-\hbeans.dtd"><beans><beanid="hello"class="org.geoserver.hellorest.HelloResource"/><beanid="helloMapping"class="org.geoserver.rest.RESTMapping"><beanid="helloMapping"class="org.geoserver.rest.RESTMapping"><propertyname="routes"><map><entry><key><value>/hello.{format}</value></key><value>hello</value></entry></map></property></bean></beans>HelloResourcehellobean。helloMappingbeanuriuri是用來訪問資源的。上面的映射描述了資源將被定位在/rest/hello.{format}format是代表客戶端請求格式。就像實現(xiàn)程序里的那樣,hello.txt是唯一受支持的表達格式。測試hello_rest模塊根部創(chuàng)建/src/test/java目錄。[hello_rest]%mkdir-psrc/test/java[hello_rest]%mkdir-psrc/test/javaorg.geoserver.hellorestHelloResourceTest的測試類,該類由org.geoserver.test.GeoServerTestSupport擴展而來:packageorg.geoserver.hellorest;packageorg.geoserver.hellorest;importorg.geoserver.test.GeoServerTestSupport;publicclassHelloResourceTestextendsGeoServerTestSupport{publicvoidtest()throwsException{}}為/rest/hello.txtGET請求添加一個狀態(tài),并且斷言到它等于“HelloWorld”字符串。publicvoidtest()throwsException{publicvoidtest()throwsException{assertEquals("HelloWorld",getAsString("/rest/hello.txt"));}hello_test模塊。[hello_rest]%mvninstall[hello_rest]%mvninstall基于Maps的Restful服務實現(xiàn)前一節(jié)中我們介紹了如何實現(xiàn)一個簡單的restful服務。本節(jié)將介紹如何為請求所表達的資源支持額外的格式利用已有的基類,以節(jié)省時間。org.geoserver.rest.MapResource。MapResource的目的是資源由包含在java.util.Mapmap集合中,MapResourceXML或者JSON格式為資源創(chuàng)建表達形式。預置條件本節(jié)建立在上一節(jié)中\(zhòng)hRESTful服務實現(xiàn)的實例基礎上。創(chuàng)建新的資源類org.geoserver.hellorestHelloMapResourceMapResource擴展:packageorg.geoserver.hellorest;packageorg.geoserver.hellorest;importjava.util.Map;importorg.geoserver.rest.MapResource;publicclassHelloMapResourceextendsMapResource{@OverridepublicMapgetMap()throwsException{returnnull;}}getMap()map的數(shù)據(jù)結(jié)構(gòu)用來表keyHelloWorld”字符串:@Override@OverridepublicMapgetMap()throwsException{HashMapmap=newHashMap();map.put("message","HelloWorld");returnmap;}更新應用程序環(huán)境(上下文)GeoServer前面所創(chuàng)建的新的資源。更新applicationContext.xml文件,如下所示:<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"\h"/dtd/spring-\hbeans.dtd"><beans><beanid="hello"class="org.geoserver.hellorest.HelloResource"/><beanid="helloMap"class="org.geoserver.hellorest.HelloMapResource"/><beanid="helloMapping"class="org.geoserver.rest.RESTMapping"><propertyname="routes"><propertyname="routes"><map><entry><key><value>/hello.{format}</value></key><!--value>hello</value--><value>helloMap</value></entry></map></property></bean></beans>這段中有兩件事需要注意。第一個是helloMapbean的添加。第二個是helloMappingbean的修改,它現(xiàn)在映射的是helloMapbean,而非hellobean。測試在org.geoserver.hellorest包中創(chuàng)建一個新的名為HelloMapResourceTest測試類,它由org.geoserver.test.GeoServerTestSupport擴展而來:packageorg.geoserver.hellorest;packageorg.geoserver.hellorest;importorg.geoserver.test.GeoServerTestSupport;publicclassHelloMapResourceTestextendsGeoServerTestSupport{publicclassHelloMapResourceTestextendsGeoServerTestSupport{}為構(gòu)建/rest/hello.xmlGETtestGetAsXML()的測試方法:importorg.w3c.dom.Document;importorg.w3c.dom.Node;...publicvoidtestGetAsXML()throwsException{//maketherequest,parsingtheresultasadomDocumentdom=getAsDOM("/rest/hello.xml");//printouttheresultprint(dom);//makeassertionsNodemessage=getFirstElementByTagName(dom,"message");assertNotNull(message);assertEquals("HelloWorld",message.getFirstChild().getNodeValue());assertEquals("HelloWorld",message.getFirstChild().getNodeValue());}為/rest/hello.jsonGETtestGetAsJSON()的測試方法。...importnet.sf.json.JSON;importnet.sf.json.JSONObject;...publicvoidtestGetAsJSON()throwsException{//maketherequest,parsingtheresultintoajsonobjectJSONjson=getAsJSON("/rest/hello.json");//printouttheresultprint(json);//makeassertionsassertTrue(jsoninstanceofJSONObject);assertEquals("HelloWorld",((JSONObject)json).get("message"));}hello_tes模塊。[hello_rest]%mvncleaninstall-Dtest=HelloMapResourceTest[hello_rest]%mvncleaninstall-Dtest=HelloMapResourceTest基于反射的Restful服務實現(xiàn)MapResourceRESTful該類是org.geoserver.rest.ReflectiveResource。該類的基本思想是資源由任意一個對象來提供。ReflectiveResource類使用反射來自動創(chuàng)建XML或者JSON格式的資源結(jié)果表達。預置條件本節(jié)建立在上一節(jié)\h基于Maps的Restful服務實現(xiàn)的示例的基礎上。創(chuàng)建一個新的Javabean ReflectiveResourceorg.geoserver.hellorest包中Hello的類:packageorg.geoserver.hellorest;packageorg.geoserver.hellorest;publicclassHello{Stringmessage;publicHello(Stringmessage){this.message=message;this.message=message;}publicStringgetMessage(){returnmessage;}}創(chuàng)建新的資源類org.geoserver.hellorestHelloReflectiveResourceReflectiveResource類擴展而來。packageorg.geoserver.hellorest;packageorg.geoserver.hellorest;importorg.geoserver.rest.ReflectiveResource;publicclassHelloReflectiveResourceextendsReflectiveResource{@OverrideprotectedObjecthandleObjectGet()throwsException{returnnull;}}}handleObjectGet()。該方法的目的是返回基礎類以表達資源。本Hello類的實例。@Override@OverrideprotectedObjecthandleObjectGet()throwsException{returnnewHello("HelloWorld");}更新應用程序環(huán)境GeoServer前一節(jié)中所創(chuàng)建的新的資源。更新applicationContext.xml文件,如下所示:<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"\h"/dtd/spring-\hbeans.dtd"><beans><beanid="hello"class="org.geoserver.hellorest.HelloResource"/><beanid="helloMap"class="org.geoserver.hellorest.HelloMapResource"/><beanid="helloReflective"class="org.geoserver.hellorest.HelloReflectiveResource"/><beanid="helloMapping"class="org.geoserver.rest.RESTMapping"><propertyname="routes"><propertyname="routes"><map><entry><key><value>/hello.{format}</value></key><!--value>hello</value--><!--value>helloMap</value--><value>helloReflective</value></entry></map></property></bean></beans>helloReflectivebeanhelloMappingbean的修helloReflectivebean。測試org.geoserver.hellorestHelloReflectiveResourceTest的類,它由org.geoserver.test.GeoServerTestSupport擴展而來:packageorg.geoserver.hellorest;packageorg.geoserver.hellorest;importorg.geoserver.test.GeoServerTestSupport;importorg.geoserver.test.GeoServerTestSupport;publicclassHelloReflectiveResourceTestextendsGeoServerTestSupport{}為構(gòu)建/rest/hello.xmlGETtestGetAsXML()的方法。importorg.w3c.dom.Document;importorg.w3c.dom.Node;...publicvoidtestGetAsXML()throwsException{//maketherequest,parsingtheresultasadomDocumentdom=getAsDOM("/rest/hello.xml");//printouttheresultprint(dom);//makeassertions//makeassertionsNodemessage=getFirstElementByTagName(dom,"message");assertNotNull(message);assertEquals("HelloWorld",message.getFirstChild().getNodeValue());}為/rest/hello.jsonGETtestGetAsJSON()的方法。importnet.sf.json.JSON;importnet.sf.json.JSONObject;...publicvoidtestGetAsJSON()throwsException{//maketherequest,parsingtheresultintoajsonJSONjson=getAsJSON("/rest/hello.json");//printouttheresultprint(json);//makeassertions//makeassertionsassertTrue(jsoninstanceofJSONObject);JSONObjecthello=((JSONObject)json).getJSONObject("org.geoserver.hellorest.Hello");assertEquals("HelloWorld",hello.get("message"));}hello_test模塊。[hello_rest]%mvncleaninstall
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 創(chuàng)新素養(yǎng)視角下高中數(shù)學高效課堂的構(gòu)建
- 中學教育基礎知到課后答案智慧樹章節(jié)測試答案2025年春白城師范學院
- 三級人力資源管理師-《企業(yè)人力資源管理師(理論知識)》考前強化模擬卷1
- 高考物理課標版一輪復習考點規(guī)范練19動量動量定理動量守恒定律的理解
- 重慶市復旦中學2024-2025學年高一下學期開學考試生物試題(原卷版)
- 揭陽中式花園施工方案
- 湖南省衡陽縣三中2017-2018學年高二上學期期中考試歷史試卷
- 廣東省惠州市惠東中學2017-2018學年高三5月熱身綜合練習生物(二)答案
- 浙江省寧波市2024-2025學年高三上學期期末模擬檢測語文試題(原卷版+解析版)
- 河南省南陽市鎮(zhèn)平縣2024-2025學年七年級上學期期末語文試題(原卷版+解析版)
- 邊坡開挖施工要求
- 數(shù)字圖像處理-6第六章圖像去噪課件
- 監(jiān)理施工設計圖紙簽發(fā)表
- 部編版六年級下冊語文教案(全冊)
- 2022年湖北成人學士學位英語真題及答案
- DB43∕T 801-2013 二次張拉低回縮鋼絞線豎向預應力短索錨固體系設計、施工和驗收規(guī)范
- 附表1:網(wǎng)絡及信息安全自查表
- 奇妙的海洋生物
- 公共場所健康證體檢表
- 普通高等學校獨立學院教育工作合格評估指標體系(第六稿)
- 多維閱讀第13級—A Stolen Baby 小猩猩被偷走了
評論
0/150
提交評論