5分钟用Spring4 搭建一个REST WebService(转)

简介: 章节目录 前置技能 新建项目,配置依赖文件 编写Model和Controller 启动服务&访问 但是 其他 前置技能 ① 使用maven来管理java项目 这个技能必须点一级,以便快速配置项目。

章节目录

前置技能

使用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

相关文章
|
3天前
|
XML 存储 Java
SpringBoot集成WebService
SpringBoot集成WebService
19 1
|
4月前
|
缓存 Java 关系型数据库
Spring Boot实现RESTful接口架构实战(包括REST的讲解、定义、REST服务测试)
Spring Boot实现RESTful接口架构实战(包括REST的讲解、定义、REST服务测试)
52 0
|
6月前
|
Java API 网络架构
Spring Boot - Rest VS GraphQL
Spring Boot - Rest VS GraphQL
32 0
|
Java Apache Spring
SpringBoot实战(十)之使用Spring Boot Actuator构建RESTful Web服务
一、导入依赖 4.0.0 org.springframework gs-actuator-service 0.1.0 org.springframework.
1784 0
|
Web App开发 JavaScript Java
Spring Boot 实现RESTful webservice服务端实例
Spring Boot 实现RESTful webservice服务端实例
3542 0
|
XML Java Apache
CXF+Spring+Hibernate实现RESTful webservice服务端实例
CXF+Spring+Hibernate实现RESTful webservice服务端实例
1552 0
|
Java 测试技术 API
Spring Boot REST API 自动化测试
Spring Boot REST API 自动化测试
3247 0
|
Java Spring 应用服务中间件
spring-boot整合Cxf的webservice案例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.Maven Plugin管理 1 2 5 4.0.0 6 7 spring-boot-ws 8 spring-boot-ws 9 1.
2493 0
|
XML Java 数据格式

热门文章

最新文章