春季休息ful 网页 服务JSON示例
- 网络服务介绍
- 网页 服务面试问题
- SOAP 网页 服务介绍
- RESTful 网页 服务介绍
- SOAP和REST 网页 服务之间的区别
- 使用Eclipse的Java中的SOAP 网页 服务示例
- JAX-WS 网页 服务Eclipse教程
- 在Tomcat上部署JAX-WS 网页 服务
- 使用Jerse在Java(JAX-RS)中创建RESTful 网页 服务
- y使用球衣的RESTful 网页 服务JAXRS JSON示例
- RESTful 网页 服务
- 使用球衣的JAXRS CRUD示例
- AngularJS RESTful 网页 服务
- 使用$ http 的JAXRS CRUD示例
- RESTful 网页 服务(JAX-RS)@QueryParam示例
- 春季休息简单示例
- 春季休息 JSON示例
- 春季休息 XML示例
- 春季休息 CRUD示例
在上一篇文章中,我们创建了一个非常简单的竞彩篮球分析 休息 ful 网页 服务,该服务返回纯文本。在本文中,我们将看到竞彩篮球分析 休息 ful 网页 服务,它将返回json作为示例。如果您想与hibernate和mysql完全集成,可以通过 春季休息ful 冬眠 MySQL示例.
这是创建简单的竞彩篮球分析 休息 ful 网页 服务的步骤,该服务将返回json。
1) 创建一个 在Eclipse中使用Maven的动态Web项目.
1 2 3 4 5 6 7 |
<依赖> <groupId> com .fastxml.杰克逊. 核心 </groupId> <artifactId>杰克逊-数据绑定</artifactId> <版>2.4.1</版> </依赖> |
现在,按如下所示更改pom.xml:
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<项目 xmlns ="http://maven.apache.org/POM/4.0.0" xmlns : si ="http://www.w3.org/2001/XMLSchema-instance" si :schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId> com .Arpit. 爪哇 2blog</groupId> <artifactId>弹簧RestfulWebServicesWithJSONExample</artifactId> <打包> 战争 </打包> <版>0.0.1-快照</版> < 名称 >弹簧RestfulWebServicesWithJSONExample 马文 网络应用</ 名称 > < 网址 > http ://maven.apache.org</url> <依存关系> <依赖> <groupId>unit</groupId> <artifactId>unit</artifactId> <版>3.8.1</版> <范围> 测试 </范围> </依赖> <依赖> <groupId> 爪哇 x .小服务程序</groupId> <artifactId> 爪哇 x .小服务程序- api </artifactId> <版>3.1.0</版> </依赖> <依赖> <groupId> 组织 .弹簧框架</groupId> <artifactId>弹簧- 核心 </artifactId> <版>${弹簧.版}</版> </依赖> <依赖> <groupId> 组织 .弹簧框架</groupId> <artifactId>弹簧- 网路 多媒体 </artifactId> <版>${弹簧.版}</版> </依赖> <依赖> <groupId> com .fastxml.杰克逊. 核心 </groupId> <artifactId>杰克逊-数据绑定</artifactId> <版>2.4.1</版> </依赖> </依存关系> <建立> <finalName>弹簧RestfulWebServicesWithJSONExample</finalName> <外挂程式> <插入> <groupId> 组织 .阿帕奇.专家.外挂程式</groupId> <artifactId>专家-编译器-插入</artifactId> <版>3.1</版> <组态> <资源>${ 杰克 .版}</资源> <目标>${ 杰克 .版}</目标> </组态> </插入> </外挂程式> </建立> <属性> <弹簧.版>4.2.1。发布</弹簧.版> < 杰克 .版>1.7</ 杰克 .版> </属性> </项目> |
弹簧应用程序配置:
3) 如下更改web.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!DOCTYPE 网路 - 应用程式 上市 “-// Sun Microsystems,Inc.//DTD 网页 应用程序2.3 // EN” "http://java.sun.com/dtd/web-app_2_3.dtd" > < 网路 - 应用程式 > <显示- 名称 >原型 已建立 网页 应用</显示- 名称 > <小服务程序> <小服务程序- 名称 >弹簧座 </小服务程序- 名称 > <小服务程序-类> 组织 .弹簧框架. 网路 .小服务程序.分派器 </小服务程序-类> < 加载 - 上 -启动>1</ 加载 - 上 -启动> </小服务程序> <小服务程序-映射> <小服务程序- 名称 >弹簧座 </小服务程序- 名称 > < 网址 -模式>/</ 网址 -模式> </小服务程序-映射> </ 网路 - 应用程式 > |
4) create a xml file 名称 d 弹簧座 -servlet.xml 在 /WEB-INF/ folder.
如果您想使用不同的弹簧包来搜索控制器,请更改context:component-scan。请参考 春季MVC 你好世界示例 了解更多。
1 2 3 4 5 6 7 8 9 10 11 12 |
<豆子 xmlns ="http://www.springframework.org/schema/beans" xmlns :语境="http://www.springframework.org/schema/context" xmlns : 多媒体 ="http://www.springframework.org/schema/mvc" xmlns : si ="http://www.w3.org/2001/XMLSchema-instance" si :schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> < 多媒体 :注解-驱动的/> <语境:零件- 扫描 基础 -包=“ 组织 .arpit.java2blog.controller” /> </豆子> |
创建bean类
5) 创建一个bean名称“Country.java”在org.arpit.java2blog.bean中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
包 组织 .Arpit. 爪哇 2blog. 豆 ; 上市 类 国家{ 整型 ID ; 串 国家的名字; 上市 国家( 整型 i, 串 国家的名字) { 超 (); 这个 . ID = i; 这个 .国家的名字 = 国家的名字; } 上市 整型 getId () { 返回 ID ; } 上市 虚空 设置名称 ( 整型 ID ) { 这个 . ID = ID ; } 上市 串 getCountryName() { 返回 国家的名字; } 上市 虚空 setCountryName(串 国家的名字) { 这个 .国家的名字 = 国家的名字; } } |
创建控制器
6) 创建一个名为的控制器“CountryController.java”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
包 组织 .Arpit. 爪哇 2blog.控制者; 进口 爪哇 . 实用程序 .数组列表; 进口 爪哇 . 实用程序 . 清单 ; 进口 组织 .Arpit. 爪哇 2blog. 豆 .国家; 进口 组织 .弹簧框架. 网路 . 捆绑 .注解.路径变量; 进口 组织 .弹簧框架. 网路 . 捆绑 .注解.请求映射; 进口 组织 .弹簧框架. 网路 . 捆绑 .注解.请求方法; 进口 组织 .弹簧框架. 网路 . 捆绑 .注解. 休息 Controller; @ 休息 Controller 上市 类 国家Controller { @请求映射( 值 = “ /国家”, 方法 = 请求方法. 得到 ,标头="接受= 应用程式 lication / json") 上市 清单 getCountries() { 清单 listOfCountries = 新 数组列表(); listOfCountries=createCountryList(); 返回 listOfCountries; } @请求映射( 值 = “ /国家/ {id}”, 方法 = 请求方法. 得到 ,标头="接受= 应用程式 lication / json") 上市 国家 getCountryById(@路径变量 整型 ID ) { 清单 listOfCountries = 新 数组列表(); listOfCountries=createCountryList(); 对于 (国家 国家: listOfCountries) { 如果 (国家.getId () == ID ) 返回 国家; } 返回 空值 ; } //使用Utiliy方法创建国家/地区列表。 上市 清单 createCountryList() { 国家 印度国家= 新 国家(1, “印度”); 国家 中国国家= 新 国家(4, “中国”); 国家 尼泊尔国家= 新 国家(3, “尼泊尔”); 国家 不丹国家= 新 国家(2, “不丹”); 清单 listOfCountries = 新 数组列表(); listOfCountries. 加 (印度国家); listOfCountries. 加 (中国国家); listOfCountries. 加 (尼泊尔国家); listOfCountries. 加 (不丹国家); 返回 listOfCountries; } } |
@PathVariable: 用于将URL中的值注入到方法参数中。通过这种方式,您可以在getCountryById方法中注入id。
我们不提供任何视图信息 弹簧座 -servlet.xml as we do 在 春季MVC。 如果我们需要直接从控制器获取资源,则需要按照竞彩篮球分析 3返回@ResponseBody,但对于竞彩篮球分析 4,我们可以使用@RestController。
在竞彩篮球分析 4.0中,我们可以使用@RestController,它是@Controller + @ResponseBody的组合。
1 2 3 |
@ 休息 Controller = @控制者 + @ResponseBody |
6) It ‘是时候进行Maven构建了。
运行应用程序
当您运行应用程序时,您可能会收到这种警告
1 2 3 4 |
三月 26, 2016 1:45:51 上午 组织 .弹簧框架. 网路 .小服务程序.找不到网页 noHandlerFound 警告: 没有 映射 发现 对于 HTTP 请求 与 URI [/弹簧RestfulWebServicesWithJSONExample/] 在 分派器 与 名称 '竞彩篮球分析RestfulWebServicesWithJSONExample' |
请忽略以上警告。启动应用程序时,如果未提供起始页,则位于URL下方:
http :// localhost:8080 / 弹簧RestfulWebServicesWithJSONExample /
当我们在web.xml中使用DispatcherServlet时,此请求转到spring 分派器,并且未在controller中找到相应的映射,因此收到该警告。
9) 在以下条件下测试您的REST服务:“http:// localhost:8080 / 弹簧RestfulWebServicesWithJSONExample / countries”.
那’关于竞彩篮球分析 休息 ful 网页 服务json示例的全部内容。