SpringBoot实战(一)之构建RestFul风格
RestFul风格是一种非常流行的架构风格,相关实战可以参考我的这篇博客:SSM框架之RestFul示例
论文可参考:https://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
关于论文,我想说的是,程序员不要对英文产生恐惧感,现在的翻译工具多的多,当然了,多也代表一些杂碎的也不少,所以就需要我们学会如何甄别。
我英语也不好,不过我目前也在学会如何看英文文档,其实英文并没有那么可怕,相反,它还是很有趣的,毕竟我们天天对诸如Eclipse或IDEA这样的英文软件,而且还写着一大堆的英文代码,日子久了,自然都知道是什么意思了。
记得唐代有句古诗:熟读唐诗三百首,不会做诗也会吟。
对于天天敲着英文代码的我们而言,也是如此。每个人都有一个过程,过程周期有长有短。
在这里再闲扯一句,外国人的技术创新能力不容小觑,我们现在用的很多技术,都已经是人家用了好多年甚至已经过时了的,随着经济全球化越来越广越来越深,而且目前国内的培训机构和大学生研究生硕士博士等,如果不想被淘汰,必须要掌握强大学习能力。其中有一项就是英文要会。先不说会不会说,发音标不标准,至少要看的懂是什么意思吧。
总而言之,送我自己和大家一句话,循序渐进。
构建环境为:JDK8+MAVNE3以上+Eclipse
本示例参考Spring官方文档:https://spring.io/guides/gs/rest-service/
一、构建maven依赖
<?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.5.8.RELEASE</version>
</parent> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</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-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>
二、构建实体
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;
}
}
三、构建Controller
package hello; 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));
}
}
关于RestController和Controller存在什么不同,主要是RestController中存在一个全局的ResponseBody,从而保证返回的异步数据为JSON数据。
可参考我的博客:前后端交互之封装Ajax+SpringMVC源码分析
另外这里也说说@RequestParam,其实加这个不加这个都可以获取参数,唯一的区别是,如果不加@RequestParm,你必须要确保前台的ajax或者同步请求的参数名必须与后台Controller中对应方法上参数列表中的参数名保持一致,否则会导致参数传输不过来,从而导致某些异常错误信息。
而加了@RequestParam,你可以让前台的值不与后台一致,你只需如下即可:
例如我前台的参数名叫test,我后台加了@RequestParam(value="test") String test001,这样就可以获取对应的参数了。同时的话,我还有可以增加一个叫required的参数,required无论是在input的属性还是后台,都有一个共性叫是否必填。后台中默认是false,不必填,当为true时,为必填。
而前台html中input,加了required,如果不在对应的表单中输入信息,就会提示此表单为必填项诸如此类的信息。
提到@RequestParam时,还不得不提一个叫@PathVariable的注解,这个注解对于经常写博客的友友们非常不陌生,为什么这么说了,比如大家有没有观察到博客园的导航栏
例如:
以我为例
https://home.cnblogs.com/u/youcong/
/u,我想应该是关于用户对应的名称或参数名,而/youcong就是对应的参数值。
这和https://i.cnblogs.com/posts?page=2本质上是一样的,只是参数呈现的表现方式不一样。
从而也凸显@RequestParam和@PathVariable的区别之一。
四、构建启动类
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);
}
}
五、执行Application中的main方法即可运行一个SpringBoot的restful风格
最终结果如下图所示:
另外不得不提下SpringBootApplication的源码:
源码如下:
* Copyright 2012-2017 the original author or authors. package org.springframework.boot.autoconfigure; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.annotation.AliasFor; /**
* Indicates a {@link Configuration configuration} class that declares one or more
* {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration
* auto-configuration} and {@link ComponentScan component scanning}. This is a convenience
* annotation that is equivalent to declaring {@code @Configuration},
* {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
*
* @author Phillip Webb
* @author Stephane Nicoll
* @since 1.2.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication { /**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude")
Class<?>[] exclude() default {}; /**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
* @since 1.3.0
*/
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "excludeName")
String[] excludeName() default {}; /**
* Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}
* for a type-safe alternative to String-based package names.
* @return base packages to scan
* @since 1.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {}; /**
* Type-safe alternative to {@link #scanBasePackages} for specifying the packages to
* scan for annotated components. The package of each class specified will be scanned.
* <p>
* Consider creating a special no-op marker class or interface in each package that
* serves no purpose other than being referenced by this attribute.
* @return base packages to scan
* @since 1.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {}; }
另外也提提@SpringBootApplication的注解包含的意思:
@Configuration
标记该类作为应用程序上下文的bean定义的源。@EnableAutoConfiguration
告诉Spring Boot开始根据类路径设置,其他bean和各种属性设置添加bean。通常你会添加
@EnableWebMvc
一个Spring MVC应用程序,但Spring Boot会在类路径上看到spring-webmvc时自动添加它。这会将应用程序标记为Web应用程序并激活关键行为,例如设置aDispatcherServlet
。@ComponentScan
告诉Spring在包中寻找其他组件,配置和服务hello
,允许它找到控制器。
SpringBoot实战(一)之构建RestFul风格的更多相关文章
- SpringBoot实战(二)Restful风格API接口
在上一篇SpringBoot实战(一)HelloWorld的基础上,编写一个Restful风格的API接口: 1.根据MVC原则,创建一个简单的目录结构,包括controller和entity,分别创 ...
- Spring Boot构建 RESTful 风格应用
Spring Boot构建 RESTful 风格应用 1.Spring Boot构建 RESTful 风格应用 1.1 实战 1.1.1 创建工程 1.1.2 构建实体类 1.1.4 查询定制 1.1 ...
- 构建RESTful风格的WCF服务
构建RESTful风格的WCF服务 RESTful Wcf是一种基于Http协议的服务架构风格. 相较 WCF.WebService 使用 SOAP.WSDL.WS-* 而言,几乎所有的语言和网络平台 ...
- SpringMVC 构建Restful风格 及问题处理
基本的请求URL: /person/{id} GET 得到id的person /person POST 新增person /person/{id} PUT 更新id的person / ...
- lucene构建restful风格的简单搜索引擎服务
来自于本人博客: lucene构建restful风格的简单搜索引擎服务 本人的博客如今也要改成使用lucene进行全文检索的功能,因此在这里把代码贴出来与大家分享 一,文件夹结构: 二,配置文件: 总 ...
- Spring Boot 中 10 行代码构建 RESTful 风格应用
RESTful ,到现在相信已经没人不知道这个东西了吧!关于 RESTful 的概念,我这里就不做过多介绍了,传统的 Struts 对 RESTful 支持不够友好 ,但是 SpringMVC 对于 ...
- Spring Boot2 系列教程(三十一)Spring Boot 构建 RESTful 风格应用
RESTful ,到现在相信已经没人不知道这个东西了吧!关于 RESTful 的概念,我这里就不做过多介绍了,传统的 Struts 对 RESTful 支持不够友好 ,但是 SpringMVC 对于 ...
- springmvc+swagger构建Restful风格文档
本次和大家分享的是java方面的springmvc来构建的webapi接口+swagger文档:上篇文章分享.net的webapi用swagger来构建文档,因为有朋友问了为啥.net有docpage ...
- springMVC+json构建restful风格的服务
首先.要知道什么是rest服务,什么是rest服务呢? REST(英文:Representational State Transfer,简称REST)描写叙述了一个架构样式的网络系统.比方 web 应 ...
随机推荐
- MongoDB客户端及监控工具
现在许多应用都使用MongoDB存储大量的业务数据,MongoDB基于文档式的存储,在大数据行业的应用还是很普遍的.MongoDB的客户端工具也很多,基于web的却不多,国产的就更少了,下面这款国产的 ...
- C#语言数据类型
整数类型 sbyte -128~127之间 byte 0~255 short(Int16) -32768~32768 ushort(UInt16) 0~65535 Int (Int32) ...
- 可持久化trie(BZOJ5338: [TJOI2018]xor)
题面 BZOJ Sol 显然是要维护一个区域的 \(trie\) 树,然后贪心 区间 \(trie\) 树??? 可持久化 \(trie\) 树??? 直接参考主席树表示出区间的方法建立 \(trie ...
- Windows安装ActiveMQ记录
1.下载压缩包(activeMQ应用要基于jdk服务上,安装本软件时,最好已经安装了jdk并且配置好了环境变量) 下载5.12.2版本:http://activemq.apache.org/activ ...
- CSS3字体火焰燃烧效果
动画的CSS: // fire @keyframes fireDiv { 0% { text-shadow: 0 0 4px white, 0 -5px 4px #ff3, 2px -10px 6px ...
- Java设计模式—桥梁模式
终于又碰到了一个简单点的模式了. 桥梁模式也叫做桥接模式,定义如下: 将抽象和实现解耦,使得两者可以独立地变化. 这句话也太难理解了,桥梁模式是为了解决类继承的缺点而设计 ...
- 学习ssm心得
学习是一个探索的过程,在我编程能力提高的阶段中,我发现在编写代码之前,必须得弄清代码的逻辑,我到底要干什么,从哪一步做起.端与端之间该怎么衔接. 先把问题想清楚,再去编写.不要急着去乱敲一通,一点用处 ...
- 算法之求质数(Java语言)
质数(Prime number) 又称素数,指在的自然数中,除了1和该数自身外,无法被其他自然数整除的数(也可定义为只有1与该数本身两个因数的数). 算法原理 验证一个数字 n 是否为素数的一种简单但 ...
- django从1.7升级到1.9后 提示:RemovedInDjango110Warning
Django项目,把django从1.7升级到1.9后,大量报错.需要做如下修改. 1,修改urls.py: 在django1.9里,urls的配置不再支持字符串型的路由.需要先import,然后直接 ...
- jsonp和ajax的区别
一.Ajax工作原理 相当于在用户和服务器之间加了—个中间层(AJAX引擎),使用户操作与服务器响应异步化.对于用户请求ajax引擎会做一些数据验证和数据处理,不是所有请求都提交给服务器,当需要从服务 ...