SpringMVC REST

本文深入探讨了RESTful架构的概念,包括资源、表现层和状态转换的基本原理,并详细介绍了如何在Spring MVC框架中实现RESTful API,涵盖增删改查操作及JSON数据处理。

REST
Representational State Transfer:资源表现层状态转换,是目前比较主流的一种互联网软件架构,它的结构清晰、标准规范、易于理解、便于扩展。

资源(Resource)
网络上的一个实体,或者硕网络上中存在的一个具体的信息,一个文本,一张图片、一首歌曲、一段视频等等,总之就是一个具体的存在。可以用一个URL(统一资源定位符)指向它,每个资源都有对应的特定的URL,要获取该资源时,只需要访问对应的URL即可。

表现层(Representation)
资源的具体呈现出来的形式,必须文本可以用txt格式表示,也可以用HTML\JSON\XML等格式来表示。

状态转换(StateTransfer)
客户端如果希望操作服务器中的某个资源,就需要通过某种方式让服务端发生状态转换,而这种转换是建立在表现层之上的,所以叫作做“表现层状态转换”。

SpringMVC REST特点

  • URL更加简洁
  • 有利于不同系统之间的资源共享,只需要遵守一定的规范,不需要进行其他的配置就可以实现资源共享。

如何使用
REST具体操作就是HTTP协议中四个表示操作方式的动词分别对应CRUD基本操作。
GET 用来表示获取资源
POST 用来表示新建资源
PUT 用来表示修改资源
DELETE 用来表示删除资源

在StudentRepository接口中定义一个Map集合,用来存放Student的信息模拟数据库,在StudentRspository接口的实现类中添加学生的信息,在Handler中写增删改查的业务方法。增删改的具体操作在postman中手动实现。在postman中,如果要实现save和update方法,不是只在postman中的地址栏输入http:/localhost:8080/rest/save或者http:/localhost:8080/rest/update,不然会报错:The request sent by the client was syntactically incorrect

另外向客户端传入的是一个json对象,所以要在pom.xml中添加相关json依赖,另外在springmvc,xml中,在mvc驱动中也要添加响应的bean。不然SpringMVC返回对象类型报错:HttpMessageNotWritableException: No converter found for return value of type

pom.xml

<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.5</version>
</dependency>
<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.5</version>
</dependency>

springmvc.xml

<mvc:annotation-driven conversion-service="conversionService">
    <!-- 消息转换器(在注解驱动里面加) -->
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes" value="text/html;charset=UTF-8"></property>
        </bean>

        <bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
    </mvc:message-converters>
</mvc:annotation-driven>

Handler业务代码

@RestController
@RequestMapping("/rest")
public class RESTHandler {

    //根据对象类型来将对象注入IOC容器
    @Autowired
    private StudentRepository studentRepository;

//    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    //该注解同上
    @GetMapping("/findAll")
    public Collection<Student> findAll(HttpServletResponse response){
        //在springmvc.xml中我们已经配置了解决的中文乱码的问题;但是如果返回客户端的是一个对象,就要在此设置对象中的中文乱码的问题
        response.setContentType("text/json;charset=UTF-8");
           return this.studentRepository.findAll();
    }

    @GetMapping("/findById/{id}")
    public Student findById(@PathVariable("id") Integer id){
        return studentRepository.findById(id);
    }
    @PostMapping("/save")
    public void save(@RequestBody Student student){
        studentRepository.saveOrUpdate(student);
    }
    @PutMapping("/update")
    public void update(@RequestBody Student student){
        studentRepository.saveOrUpdate(student);
    }
    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") Integer id){
        studentRepository.deleteById(id);
    }
    
}

StudentRepository接口

public interface StudentRepository {
    public Collection<Student> findAll();
    public Student findById(Integer id);
    public void saveOrUpdate(Student student);
    public void deleteById(Integer id);
}

StudentRepository接口的实现类StudentRespositoryImpl

@Repository
public class StudentRepositoryImpl implements StudentRepository{

    //定义静态集合,来模拟数据库
    private static Map<Integer, Student> studentMap;
    //在静态代码块中实例化该集合
    static {
        studentMap = new HashMap<>();
        studentMap.put(1,new Student(1,"张三",98.3));
        studentMap.put(2,new Student(2,"李四",97.9));
        studentMap.put(3,new Student(3,"王五",99.6));
}
    @Override
    public Collection<Student> findAll() {
        return studentMap.values();
    }

    @Override
    public Student findById(Integer id) {
        return studentMap.get(id);
    }

    @Override
    public void saveOrUpdate(Student student) {
        studentMap.put(student.getId(),student);
    }

    @Override
    public void deleteById(Integer id) {
        studentMap.remove(id);
    }
}
该数据集通过合成方式模拟了多种发动机在运行过程中的传感器监测数据,旨在构建一个用于机械系统故障检测的基准资源,特别适用于汽车领域的诊断分析。数据按固定时间间隔采集,涵盖了发动机性能指标、异常状态以及工作模式等多维度信息。 时间戳:数据类型为日期时间,记录了每个数据点的采集时刻。序列起始于2024年12月24日10:00,并以5分钟为间隔持续生成,体现了对发动机运行状态的连续监测。 温度(摄氏度):以浮点数形式记录发动机的温度读数。其数值范围通常处于60至120摄氏度之间,反映了发动机在常规工况下的典型温度区间。 转速(转/分钟):以浮点数表示发动机曲轴的旋转速度。该参数在1000至4000转/分钟的范围内随机生成,符合多数发动机在正常运转时的转速特征。 燃油效率(公里/升):浮点型变量,用于衡量发动机的燃料利用效能,即每升燃料所能支持的行驶里程。其取值范围设定在15至30公里/升之间。 振动_X、振动_Y、振动_Z:这三个浮点数列分别记录了发动机在三维空间坐标系中各轴向的振动强度。测量值标准化至0到1的标度,较高的数值通常暗示存在异常振动,可能与潜在的机械故障相关。 扭矩(牛·米):以浮点数表征发动机输出的旋转力矩,数值区间为50至200牛·米,体现了发动机的负载能力。 功率输出(千瓦):浮点型变量,描述发动机单位时间内做功的速率,取值范围为20至100千瓦。 故障状态:整型分类变量,用于标识发动机的异常程度,共分为四个等级:0代表正常状态,1表示轻微故障,2对应中等故障,3指示严重故障。该列作为分类任务的目标变量,支持基于传感器数据预测故障等级。 运行模式:字符串类型变量,描述发动机当前的工作状态,主要包括:怠速(发动机运转但无负载)、巡航(发动机在常规负载下平稳运行)、重载(发动机承受高负荷或高压工况)。 数据集整体包含1000条记录,每条记录对应特定时刻的发动机性能快照。其中故障状态涵盖从正常到严重故障的四级分类,有助于训练模型实现故障预测与诊断。所有数据均为合成生成,旨在模拟真实的发动机性能变化与典型故障场景,所包含的温度、转速、燃油效率、振动、扭矩及功率输出等关键传感指标,均为影响发动机故障判定的重要因素。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值