javascript
5分钟用Spring4 搭建一个REST WebService(转)
章節目錄
前置技能
① 使用maven來管理java項目
這個技能必須點一級,以便快速配置項目。
本文實際上是我學習Spring的過程中搬的官網上的demo,使用maven配置項目。
② jdk 1.8+?? 該服務demo需要在jdk1.8+的環境下運行
新建項目,配置依賴文件
① 安裝好eclipse的maven插件后,使用file——new——other——maven——maven project 新建一個空的maven項目。
② 訪問Spring官網demo http://spring.io/guides/gs/rest-service/
將Build With Maven 模塊中提供的pom內容復制到我們自己的pom文件中,注意將項目坐標替換成上圖中的配置。
最終得到pom文件:
<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>com.sogou.testspring</groupId> <artifactId>testspring-demo</artifactId> <version>0.0.1-SNAPSHOT</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-thymeleaf</artifactId> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestone</id> <url>https://repo.spring.io/libs-release</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestone</id> <url>https://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories> </project>編寫Model和Controller
在source目錄 src/main/java下新建hello包,在hello包下新建
① Greeting.java
package hello;public class Greeting {private final long id; private final String content; public Greeting(long id, String content) { this.id = id; this.content = content; } public long getId() { return id; } public String getContent() { return content; } }② GreetingController.java
?
package hello;import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicLong; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.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(value="/greeting2") public String Greeting2(@RequestParam(value="name",defaultValue="meiyou") String name){ return name; } @RequestMapping("/greeting3") public List<Greeting> greeting3(@RequestParam(value="name", defaultValue="World") String name) { List<Greeting> list=new ArrayList<Greeting>(); list.add(new Greeting(counter.incrementAndGet(), String.format(template, name))); list.add(new Greeting(counter.incrementAndGet(), String.format(template, name))); return list; } }③ 啟動服務? Application.java
package hello;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }官網的demo至此為止,可以發現這個demo并不需要配置web.xml或者spring dispatherServlet的xml文件,也需要依賴web容器就可以運行。 看起來這更像是一個普通的Java應用程序。
啟動服務&訪問
① 因為在pom中配置了插件
<plugin><groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>所以我們可以直接 runAs——maven build,target輸入 spring-boot:run? 來啟動服務
② 通過maven package打包項目,生成jar包,然后在target目錄下? java -jar? jar包名稱,即可啟動項目
但是
原搬原官網的demo,啟動的時候會報錯,順著報錯信息找下去,發現
spring-boot-autoconfigure-1.2.5.RELEASE.jar包下
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties 類中
有這么一段代碼
public static final String DEFAULT_PREFIX = "classpath:/templates/";public static final String DEFAULT_SUFFIX = ".html";看樣子需要一個templates的目錄,于是在resources目錄下新建一個templates目錄,再次啟動成功。
在瀏覽器中訪問:
http://localhost:8080/greeting
{"id":2,"content":"Hello, World!"}
http://localhost:8080/greeting2 meiyou
http://localhost:8080/greeting3 [{"id":3,"content":"Hello, World!"},{"id":4,"content":"Hello, World!"}]
其他
注意到demo的控制器中使用了一個注解 @RestController ,這個在spring3.x里邊是沒有的。
/** Copyright 2002-2014 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.web.bind.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.stereotype.Controller; /** * A convenience annotation that is itself annotated with {@link Controller @Controller} * and {@link ResponseBody @ResponseBody}. * <p> * Types that carry this annotation are treated as controllers where * {@link RequestMapping @RequestMapping} methods assume * {@link ResponseBody @ResponseBody} semantics by default. * * @author Rossen Stoyanchev * @author Sam Brannen * @since 4.0 */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Controller @ResponseBody public @interface RestController { /** * The value may indicate a suggestion for a logical component name, * to be turned into a Spring bean in case of an autodetected component. * @return the suggested component name, if any * @since 4.0.1 */ String value() default ""; }可以看到該注解是一個類注解,其功能相當于同時應用了? @Controller 和 @ResponseBody
http://www.cnblogs.com/tzyy/p/4837701.html
總結
以上是生活随笔為你收集整理的5分钟用Spring4 搭建一个REST WebService(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Android先进】查看手机记忆库状态
- 下一篇: Spring事务处理,以及Spring事