spring web之 Building a RESTful Web Service(创建restful web 服务)和@RestController

1.restful web service 理解

1.我们以前MVC的返回数据都是通过一个View去返回一些数据,现在我们只需要返回一个对象等;
2.可能觉得很抽象,我们下面通过一个案例去理解

2.案例

2.1.引入pom.xml

<?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>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.5.RELEASE</version>
	</parent>

  <groupId>com.gaoxinfu.demo.spring.web</groupId>
  <artifactId>demo-spring-web</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>demo-spring-web</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
	  <!--web container -->
	  <dependency>
		  <groupId>org.springframework.boot</groupId>
		  <artifactId>spring-boot-starter-web</artifactId>
	  </dependency>

	  <!--bootstrap.yml load -->
	  <dependency>
		  <groupId>org.springframework.cloud</groupId>
		  <artifactId>spring-cloud-context</artifactId>
		  <version>2.2.2.RELEASE</version>
	  </dependency>
  </dependencies>

  <build>
    <plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
  </build>
</project>

这里其实主要是parent和web的引入

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.5.RELEASE</version>
	</parent>

 <dependency>
		  <groupId>org.springframework.boot</groupId>
		  <artifactId>spring-boot-starter-web</artifactId>
	  </dependency>

2.2.创建controller

package com.gaoxinfu.demo.spring.web.restservice;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: gaoxinfu
 * @Date: 2021-01-12 09:31
 */
@RestController
@RequestMapping(path = "/greet")
public class GreetingController {

	private static final String template = "Hello, %s!";
	private final AtomicLong counter = new AtomicLong();

	@GetMapping("/hello")
	public Greeting hello(@RequestParam(value = "name", defaultValue = "World") String name) {
		return new Greeting(counter.incrementAndGet(), String.format(template, name));
	}
}

/**
 * @Description:
 * @Author: gaoxinfu
 * @Date: 2021-01-12 09:30
 */
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;
	}
}

2.3.解析

2.3.1.@GetMapping

1.@GetMapping确保Http Get类型的请求/greet/hello能够匹配到方法hello()
2.RequestParam 可以参考下面的地址介绍,不再赘述
  https://blog.csdn.net/u014636209/article/details/112478682
3.当然HTTP请求类型,不仅仅有Get,还有Post等等请求类型

在这里插入图片描述

2.3.2.@RestController

1.对于返回值,我们定义了一个内部对象类,去封装了其属性(id和content)
2.从上面的返回值 new Greeting(),我们可以发现,传统的MVC Controller和Restful Web Controller的区别很明显的我们发现,
  其实就是返回值Response Body的创建方式的区别;
  传统MVC Controller:通过通过View技术去将greeting数据渲染成html
  	                (relying on a view technology to perform server-side rendering of the greeting data to HTML)
  Restful Web Controller:通过返回个Greeting对象,对象将被直接通过json的方式写入到Http的reponse之中;
3.@RestController = @Controller + @ResponseBody 
  @RestController注解作为类上面,标示这个每一个方法可以返回一个domain object 对象替代传统mvc的view视图;
4.由于Spring的Http 消息message转换器的支持,我们不需要手动的做任何的转换,就可以将Greeting对象转换为JSON;
  在classpath下Jackson 2 会被加载,Spring的 MappingJackson2HttpMessageConverter自动的被选择使用,将the Greeting转换为JSON 

在这里插入图片描述

3.源码地址

https://gitee.com/gaoxinfu_admin/open-source/tree/master/spring/spring-framework-5.0.2.RELEASE-%E4%B8%AD%E6%96%87%E6%B3%A8%E9%87%8A%E7%89%88/demo-spring-web/src/main/java/com/gaoxinfu/demo/spring/web/restservice

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东山富哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值