Spring之路(25)–Spring Restful+jQuery+Bootstrap开发博客系统实例(API后端开发篇)

本文涉及的产品
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
RDSClaw,2核4GB
RDS DuckDB + QuickBI 企业套餐,8核32GB + QuickBI 专业版
简介: 本文目录1. 背景2. 新建项目3. 导入jar包4. 修改web.xml加载spring配置5. 新建springmvc-config.xml,配置容器信息6. 添加数据库操作类DbHelper7. 添加博客服务类BlogService与数据对象BlogBo8. BlogController实现Restful API9. 浏览器简单测试10.总结

1. 背景

本篇将之前实现过的博客系统的后端api改为Spring Restful风格,虽然说是修改,但实际上还是从头到尾实现一下子。


2. 新建项目

打开eclipse,File-New-Other-Dynamic Web Project。


Project name:restfulblog


勾选Generate web.xml deployment descriptor


3. 导入jar包

将之前一直在用的jar包拷贝到WEB-INF/lib目录下。


注意Spring Restful请求都是返回json字符串,所以需要添加转换json的jar包,同时因为要访问数据库,所以添加mysql数据链接的jar包:

image.png

后续不再逐一提供jar包下载地址,给大家一个可以搜索的jar包仓库,自行下载吧:https://mvnrepository.com/


4. 修改web.xml加载spring配置

修改web.xml如下,该配置文件配置了DispatcherServlet,同时指定了容器使用的配置文件springmvc-config.xml。


注意<url-pattern>/*</url-pattern>表示匹配所有请求,此时不再限制请求路径以.do结尾,因为跟restful风格不符合了。


<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

id="WebApp_ID" version="3.1">

<display-name>restfulblog</display-name>

<servlet>

 <servlet-name>springmvc</servlet-name>

 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

 <init-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>/WEB-INF/springmvc-config.xml</param-value>

 </init-param>

 <load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

 <servlet-name>springmvc</servlet-name>

 <url-pattern>/*</url-pattern>

</servlet-mapping>

</web-app>


5. 新建springmvc-config.xml,配置容器信息

在WEB/INF下新建springmvc-config.xml,添加对容器的配置,主要是开启bean自动扫描。另外开启了新的注解<mvc:annotation-driven />为MVC提供额外的支持。


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

       http://www.springframework.org/schema/context

       http://www.springframework.org/schema/context/spring-context-4.0.xsd

       http://www.springframework.org/schema/mvc

       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<!-- 指定扫描的包 -->

<context:component-scan

 base-package="org.maoge.restfulblog" />

<!--注解驱动,开启通过注解配置访问路径与方法的匹配 -->

<mvc:annotation-driven />

</beans>


6. 添加数据库操作类DbHelper

注意此处我们还是使用myblog数据库,不再新建数据库了,所以url依然为jdbc:mysql://127.0.0.1:3306/myblog?useUnicode=true&characterEncoding=utf-8


然后注意我们将@Component改为了@Repository,@Repository也是将DbHelper注册为bean,但是它用于持久层上,更加能表明bean的类型,一般跟持久层相关的bean使用@Repository。OK,代码如下:


package org.maoge.restfulblog;

import java.sql.Connection;

import java.sql.DriverManager;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Repository;

//@Component

@Repository//替代@Component

public class DbHelper {

@Value("com.mysql.jdbc.Driver") // 注入driver属性

private String driver;

@Value("jdbc:mysql://127.0.0.1:3306/myblog?useUnicode=true&characterEncoding=utf-8") // 注入数据库url属性

private String url;

@Value("root") // 注入用户名属性

private String username;

@Value("XXX") // 注入密码属性

private String password;

/**

 * 初始化

 */

@PostConstruct // 使用该注解,以便在项目启动时执行数据库驱动加载工作

public void init() {

 try {

  Class.forName(driver);

 } catch (Exception e) {

  // 此处应打印日志

 }

}

/**

 * 获取数据库连接

 */

public Connection getConnection() {

 Connection conn = null;

 try {

  conn = DriverManager.getConnection(url, username, password);

 } catch (Exception e) {

  // 此处应打印日志

 }

 return conn;

}

/**

 * 关闭数据库连接

 */

public void closeConnection(Connection conn) {

 if(conn!=null) {

  try {

   conn.close();

  }catch(Exception e) {

   // 此处应打印日志

  }

 }

}

}


7. 添加博客服务类BlogService与数据对象BlogBo

注意博客服务类的注解,我们将@Component改为了@Service,这个道理同上,@Service用来描述服务层(业务层)的bean。具体代码如下:


package org.maoge.restfulblog;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

/**

* @theme 服务--博客

* @author maoge

* @date 2020-01-27

*/

//@Component

@Service//替代@Component

public class BlogService {

@Autowired // 自动注入dbHelper组件

private DbHelper dbHelper;

/**

 * 获取博客列表

 */

public List<BlogDo> getBlogList() {

 List<BlogDo> blogList = new ArrayList<BlogDo>();

 Connection conn = null;

 try {

  conn = dbHelper.getConnection();

  String sql = "select * from blog";

  PreparedStatement stmt = conn.prepareStatement(sql);

  ResultSet rs = stmt.executeQuery();

  while (rs.next()) {

   BlogDo blog = new BlogDo();

   blogList.add(blog);

   blog.setAuthor(rs.getString("author"));

   blog.setContent(rs.getString("content"));

   blog.setId(rs.getLong("id"));

   blog.setTitle(rs.getString("title"));

  }

 } catch (Exception e) {

  // 此处应打印日志

 } finally {

  dbHelper.closeConnection(conn);

 }

 return blogList;

}

/**

 * 按id获取博客信息

 */

public BlogDo getBlogById(Long id) {

 BlogDo blog=null;

 Connection conn = null;

 try {

  conn = dbHelper.getConnection();

  String sql = "select * from blog where id=?";

  PreparedStatement stmt = conn.prepareStatement(sql);

  stmt.setLong(1, id);

  ResultSet rs = stmt.executeQuery();

  if (rs.next()) {

   blog=new BlogDo();

   blog.setAuthor(rs.getString("author"));

   blog.setContent(rs.getString("content"));

   blog.setId(rs.getLong("id"));

   blog.setTitle(rs.getString("title"));

  }

 } catch (Exception e) {

  // 此处应打印日志

 } finally {

  dbHelper.closeConnection(conn);

 }

 return blog;

}

/**

 * 新增博客

 */

public int addBlog(BlogDo blog) {

 Connection conn = null;

 try {

  conn = dbHelper.getConnection();

  String sql = "insert into blog(author,content,title)values(?,?,?)";

  PreparedStatement stmt = conn.prepareStatement(sql);

  stmt.setString(1, blog.getAuthor());

  stmt.setString(2, blog.getContent());

  stmt.setString(3, blog.getTitle());

  return stmt.executeUpdate();

 } catch (Exception e) {

  // 此处应打印日志

  return 0;

 } finally {

  dbHelper.closeConnection(conn);

 }

}

/**

 * 根据博客id更新博客信息

 */

public int updateBlog(BlogDo blog) {

 Connection conn = null;

 try {

  conn = dbHelper.getConnection();

  String sql = "update blog set author=?,content=?,title=? where id=?";

  PreparedStatement stmt = conn.prepareStatement(sql);

  stmt.setString(1, blog.getAuthor());

  stmt.setString(2, blog.getContent());

  stmt.setString(3, blog.getTitle());

  stmt.setLong(4, blog.getId());

  return stmt.executeUpdate();

 } catch (Exception e) {

  // 此处应打印日志

  return 0;

 } finally {

  dbHelper.closeConnection(conn);

 }

}

/**

 * 根据博客id删除对应博客

 */

public int deleteBlog(Long id) {

 Connection conn = null;

 try {

  conn = dbHelper.getConnection();

  String sql = "delete from blog where id=?";

  PreparedStatement stmt = conn.prepareStatement(sql);

  stmt.setLong(1, id);

  return stmt.executeUpdate();

 } catch (Exception e) {

  // 此处应打印日志

  return 0;

 } finally {

  dbHelper.closeConnection(conn);

 }

}

}


8. BlogController实现Restful API

重点来了,之前的控制器中,我们返回的都是网页,而在Restful的风格下,我们需要返回json。


SpringMVC可以通过对方法添加@ResponseBody注解,将返回的对象转换为json,完事了!就是这么简单,这就是极简的设计理念,完美。还可以更加简单,如果一个控制器中提供的都是Restful API方法,则可以直接将@Controller改写为@RestController,这样控制下所有API方法返回的对象都会被自动转换为json,比简单还简单,追求极致。


下面具体实现下,注意下注释:


package org.maoge.restfulblog;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.PutMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;

/**

* @theme 控制器--博客

* @author maoge

* @date 2020-01-28

*/

@RestController // 通过该注解,第一将BlogController注册为控制器,第二将其中方法返回值转换为json

public class BlogController {

@Autowired // 自动装配blogService

private BlogService blogService;

/**

 * 查询博客信息

 * 1、@GetMapping表示可以使用get方法请求该api

 * 2、"/blog/{id}"表示请求路径为/blog/{id}的形式,其中{id}为占位符

 * 3、@PathVariable("id")表示将占位符{id}的值传递给id

 * 4、也就是说/blog/123请求的话,会将123传递给参数id

 */

@GetMapping("/blog/{id}")

public BlogDo getOne(@PathVariable("id") long id) {

 return blogService.getBlogById(id);

}

/**

 * 查询博客列表,使用get方法

 */

@GetMapping("/blog")

public List<BlogDo> getList(){

 return blogService.getBlogList();

}

/**

 * 新增博客

 * 1、@PostMapping表示使用post方法

 * 2、@RequestBody表示将请求中的json信息转换为BlogDo类型的对象信息,该转换也是由SpringMVC自动完成的

 */

@PostMapping("/blog")

public void add(@RequestBody BlogDo blog) {

 blogService.addBlog(blog);

}

/**

 * 修改博客

 * 实际上此处也可以不在路径中传递id,而是整个使用json传递对象信息,但是我查询了一些文档,貌似使用路径传递id更加规范一些,此处不用纠结

 */

@PutMapping("/blog/{id}")

public void update(@PathVariable("id") long id,@RequestBody BlogDo blog) {

 //修改指定id的博客信息

 blog.setId(id);

 blogService.updateBlog(blog);

}

/**

 * 删除博客

 */

@DeleteMapping("/blog/{id}")

public void delete(@PathVariable("id") long id) {

 blogService.deleteBlog(id);

}

}


9. 浏览器简单测试

由于浏览器地址栏直接输入的请求是get类型的,所以我们可简单的从浏览器测试下查询博客信息,打开谷歌浏览器,访问http://127.0.0.1:8080/restfulblog/blog/1,返回如下信息:image.png

可见已成功返回json字符串。

10.总结

当后端可以提供规范的API,以restful风格提供服务接口,而返回值采用规范的json格式,是一个巨大的提高。

后续我们可以采用这种风格来开发web应用,前端负责界面渲染操作交互,后端提供API接口,前后端分离,更加优雅高效。

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
8月前
|
前端开发 Java API
利用 Spring WebFlux 技术打造高效非阻塞 API 的完整开发方案与实践技巧
本文介绍了如何使用Spring WebFlux构建高效、可扩展的非阻塞API,涵盖响应式编程核心概念、技术方案设计及具体实现示例,适用于高并发场景下的API开发。
624 0
|
7月前
|
人工智能 Java 机器人
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
Spring AI Alibaba集成Ollama,基于Java构建本地大模型应用,支持流式对话、knife4j接口可视化,实现高隐私、免API密钥的离线AI服务。
6282 2
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
|
存储 人工智能 Java
Spring AI与DeepSeek实战四:系统API调用
在AI应用开发中,工具调用是增强大模型能力的核心技术,通过让模型与外部API或工具交互,可实现实时信息检索(如天气查询、新闻获取)、系统操作(如创建任务、发送邮件)等功能;本文结合Spring AI与大模型,演示如何通过Tool Calling实现系统API调用,同时处理多轮对话中的会话记忆。
2651 57
|
8月前
|
缓存 Java API
Spring WebFlux 2025 实操指南详解高性能非阻塞 API 开发全流程核心技巧
本指南基于Spring WebFlux 2025最新技术栈,详解如何构建高性能非阻塞API。涵盖环境搭建、响应式数据访问、注解与函数式两种API开发模式、响应式客户端使用、测试方法及性能优化技巧,助你掌握Spring WebFlux全流程开发核心实践。
1423 0
|
10月前
|
Java API 网络架构
基于 Spring Boot 框架开发 REST API 接口实践指南
本文详解基于Spring Boot 3.x构建REST API的完整开发流程,涵盖环境搭建、领域建模、响应式编程、安全控制、容器化部署及性能优化等关键环节,助力开发者打造高效稳定的后端服务。
1302 1
|
存储 安全 Java
Spring Boot 编写 API 的 10条最佳实践
本文总结了 10 个编写 Spring Boot API 的最佳实践,包括 RESTful API 设计原则、注解使用、依赖注入、异常处理、数据传输对象(DTO)建模、安全措施、版本控制、文档生成、测试策略以及监控和日志记录。每个实践都配有详细的编码示例和解释,帮助开发者像专业人士一样构建高质量的 API。
611 9
|
Java 测试技术 API
详解Swagger:Spring Boot中的API文档生成与测试工具
详解Swagger:Spring Boot中的API文档生成与测试工具
1194 4
|
JSON 缓存 测试技术
构建高效RESTful API的后端实践指南####
本文将深入探讨如何设计并实现一个高效、可扩展且易于维护的RESTful API。不同于传统的摘要概述,本节将直接以行动指南的形式,列出构建RESTful API时必须遵循的核心原则与最佳实践,旨在为开发者提供一套直接可行的实施框架,快速提升API设计与开发能力。 ####
|
JSON API 数据格式
探索后端开发:从零构建简易RESTful API
在数字时代的浪潮中,后端开发如同搭建一座桥梁,连接着用户界面与数据世界。本文将引导读者步入后端开发的殿堂,通过构建一个简易的RESTful API,揭示其背后的逻辑与魅力。我们将从基础概念出发,逐步深入到实际操作,不仅分享代码示例,更探讨如何思考和解决问题,让每一位读者都能在后端开发的道路上迈出坚实的一步。
|
安全 测试技术 API
构建高效RESTful API:后端开发的艺术与实践####
在现代软件开发的浩瀚星空中,RESTful API如同一座桥梁,连接着前端世界的绚丽多彩与后端逻辑的深邃复杂。本文旨在探讨如何精心打造一款既高效又易于维护的RESTful API,通过深入浅出的方式,剖析其设计原则、实现技巧及最佳实践,为后端开发者提供一份实用的指南。我们不深入晦涩的理论,只聚焦于那些能够即刻提升API品质与开发效率的关键点,让你的API在众多服务中脱颖而出。 ####
173 0
下一篇
开通oss服务