1 将返回设置为produces = "application/json"

返回给客户端json格式的response。

2 对各种异常的处理

各种异常如何返回给客户端?

各种异常通过ResponseEntity返回给客户端。

3 一种通用的处理方式

3.1 定义一个Exception对象

public class UserNotFountException extends RuntimeException{
    private String userId;

public UserNotFountException(String userId) {
        this.userId = userId;
    }

public String getUserId() {
        return userId;
    }

public void setUserId(String userId) {
        this.userId = userId;
    }
}

3.2 定义对应的ExceptionHandler

@ExceptionHandler(UserNotFountException.class)
public ResponseEntity<Error> UserNotFound(UserNotFountException e){
String userId = e.getUserId();
Error error = new Error(4 , "User ("+userId+") not found");
return new ResponseEntity<Error>(error,HttpStatus.NOT_FOUND);
}

3.3 有异常的时候直接返回异常

@RequestMapping(value = "/{id}",method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<User> getUserById (@PathVariable("id") String id){
//初始化用户信息
initUserInfo();
User result = users.get(id);
HttpStatus status = result != null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
if(result == null){
throw new UserNotFountException(id);
}
return new ResponseEntity<User>(result,status);
}

参考资料:

https://blog.csdn.net/github_34889651/article/details/53705306

通过spring boot提供restful api的更多相关文章

  1. Spring Boot中Restful Api的异常统一处理

    我们在用Spring Boot去向前端提供Restful Api接口时,经常会遇到接口处理异常的情况,产生异常的可能原因是参数错误,空指针异常,SQL执行错误等等. 当发生这些异常时,Spring B ...

  2. 基于Spring Boot的RESTful API实践(一)

    1. RESTful简述    REST是一种设计风格,是一组约束条件及原则,而遵循REST风格的架构就称为RESTful架构,资源是RESTful的核心,一个好的RESTful架构,通过URL就能很 ...

  3. Spring Boot提供RESTful接口时的错误处理实践

    使用Spring Boot开发微服务的过程中,我们会使用别人提供的接口,也会设计接口给别人使用,这时候微服务应用之间的协作就需要有一定的规范. 基于rpc协议,我们一般有两种思路:(1)提供服务的应用 ...

  4. 使用 Spring Boot 构建 RESTful API

    1. 使用 Idea 创建 Spring Initializer 项目 在创建项目的对话框中添加 Web 和 Lombok,或者建立项目后在 pom.xml 中添加依赖: <dependency ...

  5. Spring Boot构建RESTful API与单元测试

    如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建议先看一下相关的内容. @Controller:修饰class,用来创建处理http请求的对象 @RestController:Spr ...

  6. Spring Boot构建RESTful API

    @Controller:修饰class,用来创建处理http请求的对象 @RestController:Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseB ...

  7. Spring Boot2 系列教程(三十一)Spring Boot 构建 RESTful 风格应用

    RESTful ,到现在相信已经没人不知道这个东西了吧!关于 RESTful 的概念,我这里就不做过多介绍了,传统的 Struts 对 RESTful 支持不够友好 ,但是 SpringMVC 对于 ...

  8. Spring Boot构建 RESTful 风格应用

    Spring Boot构建 RESTful 风格应用 1.Spring Boot构建 RESTful 风格应用 1.1 实战 1.1.1 创建工程 1.1.2 构建实体类 1.1.4 查询定制 1.1 ...

  9. 使用Spring MVC开发RESTful API

    第3章 使用Spring MVC开发RESTful API Restful简介 第一印象 左侧是传统写法,右侧是RESTful写法 用url描述资源,而不是行为 用http方法描述行为,使用http状 ...

随机推荐

  1. C++ 和 java 使用 AES CBC 128 加解密

    Java 使用jce, code: import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax ...

  2. (转)java 打印自身代码——真实世界不存在自指

    public class SelfPrint {      public static void main(String args[]) {          char s = 34;         ...

  3. popupwindow从底部弹出

    参考了网上的一些代码,自己写了个类,上代码 /** * Created by Lee on 2016/2/26. */ public class CameraPopupWindow { private ...

  4. Silverlight实例教程 - Validation客户端同步数据验证(转载)

    摘要:在Silverlight 4中,Silverlight Validation有相对的改进,本篇将介绍Silverlight 4中新加入的验证机制功能,IDataErrorInfo客户端同步验证机 ...

  5. JMX简单样例

    一:创建maven项目,在pom.xml里面增加例如以下依赖 <dependency> <groupId>com.sun.jdmk</groupId> <ar ...

  6. leetcode第一刷_Balanced Binary Tree

    二叉平衡树好火啊.差点儿每一个公司的笔试题里都有它.考了好多次我都不会,挂笔试非常有可能就是由于它.另一个它的同伙叫二叉搜索树,貌似人气比它还要高一些. 二叉平衡树是什么样的树呢.是每一个节点的左右子 ...

  7. C++语言基础(11)-多态

    一.产生背景 先看下面的例子: #include <iostream> using namespace std; //基类People class People{ public: Peop ...

  8. android studio - 隐藏编辑器上面的导航条

    菜单栏-“View”-"Navigation Bar"

  9. Python内置函数之input()

    input([prompt])input()读取标准输入并打印字符串到屏幕. 参数是自定义的提示符. 例子: >>> input('$ ') $ pwd 'pwd'

  10. centos7.4 install ss-qt5

    一切都是为了FQ,哦,说错了,是***-- 参考官网安装指南 1.新建repo文件 vim /etc//yum.repos.d/shadowssocks.repo 2.在文件中输入以下内容: [lib ...