通过spring boot提供restful api

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

原文地址:https://www.cnblogs.com/hustdc/p/10246299.html

时间: 2024-08-04 02:20:30

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

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

本文首发于个人网站:http://www.javaadu.online/,如需转载,请注明出处 使用Spring Boot开发微服务的过程中,我们会使用别人提供的接口,也会设计接口给别人使用,这时候微服务应用之间的协作就需要有一定的规范. 基于rpc协议,我们一般有两种思路:(1)提供服务的应用统一将异常包起来,然后用错误码交互:(2)提供服务的应用将运行时异常抛出,抛出自定义的业务异常,服务的调用者通过异常catch来处理异常情况. 基于HTTP协议,那么最流行的就是RESTful协议,服务提

Spring Boot构建RESTful API与单元测试

全套教程:http://git.oschina.net/didispace/SpringBoot-Learning 首先,回顾并详细说明一下在快速入门中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建议先看一下快速入门的内容. @Controller:修饰class,用来创建处理http请求的对象 @RestController:Spring4之后加入的注解,原来在@Control

spring-boot实战【04】:Spring Boot构建RESTful API

@Controller:修饰class,用来创建处理http请求的对象@RestController:Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseBody来配合,如果直接用@RestController替代@Controller就不需要再配置@ResponseBody,默认返回json格式.@RequestMapping:配置url映射 下面我们尝试使用Spring MVC来实现一组对User对象操作的RESTful API,配合注释详细说明在S

Spring Boot构建RESTful API

@Controller:修饰class,用来创建处理http请求的对象 @RestController:Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseBody来配合,如果直接用@RestController替代@Controller就不需要再配置@ResponseBody,默认返回json格式. @RequestMapping:配置url映射 RESTful API具体设计如下: 请求类型 URL 功能说明 GET /users 查询用户列表 PO

使用Spring boot开发RestFul 风格项目PUT/DELETE方法不起作用

在使用Spring boot 开发restful 风格的项目,put.delete方法不起作用,解决办法. 实体类Student @Data public class Student { private String id; private String name; private int age; private String sex; @Override public String toString() { return ToStringBuilder.reflectionToString(

Spring Boot基础-RESTfull API简单项目的快速搭建

Spring Boot基础教程1-Spring Tool Suite工具的安装 Spring Boot基础教程2-RESTfull API简单项目的快速搭建 Spring Boot基础教程3-配置文件详解:Properties和YAML Spring Boot基础教程4-配置文件-多环境配置 Spring Boot基础教程5-日志配置-logback和log4j2 源码地址:https://github.com/roncoo/spring-boot-demo 一.搭建一个简单的RESTfull

Spring Boot 实现RESTful webservice服务端示例

1.Spring Boot configurations application.yml spring: profiles: active: dev mvc: favicon: enabled: false datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/wit_neptune?createDatabaseIfNotExist=true&useUnicode=true&

Spring Boot 的常用 API 说明

1.SpringApplication 类 作用:用于启动 Spring Boot 的程序,根据传入的类的声明的注解来决定不同的启动方式 示例代码: 1 package org.ranger; 2 import org.springframework.boot.SpringApplication; 3 import org.springframework.boot.autoconfigure.SpringBootApplication; 4 //使用 SpringBoot 自动配置程序 5 @S

Spring mvc之Restful API

这是一个路径,http://127.0.0.1:8080/OperationAPI/v0.1/pins/3是API的具体网址.在RESTful架构中,每个网址代表一种资源(resource),所以网址中不能有动词,只能有名词,而且所用的名词往往与数据库的表格名对应.一般来说,数据库中的表都是同种记录的"集合"(collection),所以API中的名词也应该使用复数.http://127.0.0.1:8080表示的本地的域名,OperationAPI表示的是项目名称,v0.1表示的ap