没有合适的资源?快使用搜索试试~ 我知道了~
RESTFUL的实现-springboot2.0版本
需积分: 0 1 下载量 45 浏览量
2023-05-29
10:33:33
上传
评论
收藏 144KB PDF 举报
温馨提示
试读
11页
RESTFUL的实现-springboot2.0版本 网址:https://blog.csdn.net/chenwewi520feng/article/details/130634490 使用一个示例来说明restful的使用,是以springboot的2.0版本作为基础的
资源推荐
资源详情
资源评论
本文仅仅使用一个示例来说明restful的使用,是以springboot的2.0版本作为基础的。
基于springboot的示例,包含get、post、put和delete,以及使用resttemplate接收json。参数形式可
以使用restful,也可以使用形如:url?id=xx,使用方式不同。
通过浏览器地址访问的都是get的请求方式,post、put和delete则可能需要专门的代码或工具访问。也
就是post、put和delete不能通过浏览器的地址栏访问。
本文分为三部分,即pom.xml、代码和测试。
一、pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.5.6</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient-cache --
>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
二、源码
由于没有业务,代码比较简单,一般也不需要进行什么说明,就是实现一个CRUD。本示例没有连接数
据库,直接通过方法造一些数据放在list中。
1、UserBean
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Add typical dependencies for a web application -->
<!-- Adds Tomcat and Spring MVC, along others -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
package com.win.model;
import java.io.Serializable;
import lombok.Data;
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1946944818933250011L;
private long id;
private String name;
private int age;
private double salary;
public User() {
id = 0;
}
public User(long id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
2、CustomErrorType
3、Service接口
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (id ^ (id >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id != other.id)
return false;
return true;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ",
salary=" + salary + "]";
}
}
package com.win.util;
public class CustomErrorType {
private String errorMessage;
public CustomErrorType(String errorMessage){
this.errorMessage = errorMessage;
}
public String getErrorMessage() {
return errorMessage;
}
}
package com.win.service;
剩余10页未读,继续阅读
资源评论
一瓢一瓢的饮alanchanchn
- 粉丝: 2827
- 资源: 69
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功