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

 

REST WebService的更多相关文章

  1. webService

    什么是webService WebService,顾名思义就是基于Web的服务.它使用Web(HTTP)方式,接收和响应外部系统的某种请求.从而实现远程调用.  1:从WebService的工作模式上 ...

  2. 开始webservice了

    一.WebService到底是什么 一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 所谓跨编程语言和跨操作平台,就是说服务端程序采用java编写,客户端程序则可以采用 ...

  3. Spring WebService入门

    Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布 ...

  4. 浅谈跨域以及WebService对跨域的支持

    跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的资源,不能访问和操作其他域下的资源. 在 ...

  5. 浅谈WebService的版本兼容性设计

    在现在大型的项目或者软件开发中,一般都会有很多种终端, PC端比如Winform.WebForm,移动端,比如各种Native客户端(iOS, Android, WP),Html5等,我们要满足以上所 ...

  6. Atitit webservice发现机制 WS-Discovery标准的规范attilax总结

    Atitit webservice发现机制 WS-Discovery标准的规范attilax总结 1.1. WS-Discovery标准1 1.2. 一.WS-Discovery1 1.2.1.   ...

  7. java调用CXF WebService接口的两种方式

    通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了. 2.静态调用 // 创建WebService客户端代理工厂 ...

  8. VS2010编写WebService与在IIS的发布<之简单讲解>

    工具VS2010,window环境win7 一:Webservice的创建与方法查看调用 1.新建空web应用程序项目 2.新建web服务 3.自动生成 4.直接跑起来,可以看到有2个方法 5.点击H ...

  9. webService学习之路(三):springMVC集成CXF后调用已知的wsdl接口

    webService学习之路一:讲解了通过传统方式怎么发布及调用webservice webService学习之路二:讲解了SpringMVC和CXF的集成及快速发布webservice 本篇文章将讲 ...

  10. webService学习之路(二):springMVC集成CXF快速发布webService

    继上一篇webService入门之后,http://www.cnblogs.com/xiaochangwei/p/4969448.html ,现在我将我周六在家研究的结果公布出来 本次集成是基于之前已 ...

随机推荐

  1. QT Programming 1

    1.控制台输出 helloworld #include<QtCore/QCoreApplication> #include<QDebug> int main(int argc, ...

  2. poj Optimal Milking

    Optimal Milking 题目: 有K个机器.C仅仅牛.要求求出最全部牛到各个产奶机的最短距离.给出一个C+K的矩阵,表示各种标号间的距离. 而每一个地方最多有M仅仅牛. 算法分析: 二分+最短 ...

  3. Java Drp项目实战——Drp知多少

    是什么 Drp是Distribution Resource Planning的缩写,意思是分销资源计划.它是用来管理企业的执行于Internet上的分销网络的系统,是以商业流程优化为基础,它的核心是销 ...

  4. mysqldump 命令的使用

    1.导出结构不导出数据 mysqldump -d databasename -uroot -p > xxx.sql 2.导出数据不导出结构 mysqldump -t databasename - ...

  5. 采用ACE登录设施(一)HelloWorld

    (1)开始使用日志设施 使用日志设施,总是要包括头文件: #include "ace/Log_Msg.h" ACE日志的Hello World #ifdef _DEBUG #pra ...

  6. NOI 评价体系 arbiter 安装方法 常见的问题 移植

    #!/bin/bash AppPath="$PWD"   读取当前文件夹 echo "Arbiter is installing..." sudo apt-ge ...

  7. ZOJ 3728 Collision

    ---恢复内容开始--- 今天无事水一水,结果就看到这个水题了! 题意思是 有俩个区域如图 求在俩个圆之间的运动时间 给出 初始的开始点和速度的矢量式;而且这个点 不再俩个圆之间的区域,且碰到内测园会 ...

  8. air mobile andriod ios 页面加载控件

    通过最近的研究flex 书写android .ios申请书,我们遇到了一个问题加载页面,我们用flex sdk 12,air 15 无级似android ListView寻呼模块.所以,我和我的同事们 ...

  9. 安全扫描工具-AppScan

    AppScan主要作用是对页面进行安全扫描的工具

  10. curl 命令详解(转)

    命令事例 1.读取网页,将网页内容全部输出 curl http://www.linuxidc.com 2.保存网页.下载文件 以page.html命名下载网页:curl –o page.html ht ...