书接上文⬆⬆⬆
http 是平台 要使用 web 层次包装您的存储库,您必须使用 spring mvc。多亏了 spring boot,代码基础设施很少。相反,我们可以专注于行动:
nonrest/src/main/java/payroll/employeecontroller.java
package payroll;
import java.util.list;
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;
@restcontroller
class employeecontroller {
private final employeerepository repository;
employeecontroller(employeerepository repository) {
this.repository = repository;
}
// aggregate root
// tag::get-aggregate-root[]
@getmapping(/employees)
list all() {
return repository.findall();
}
// end::get-aggregate-root[]
@postmapping(/employees)
employee newemployee(@requestbody employee newemployee) {
return repository.save(newemployee);
}
// single item
@getmapping(/employees/{id})
employee one(@pathvariable long id) {
return repository.findbyid(id)
.orelsethrow(() -> new employeenotfoundexception(id));
}
@putmapping(/employees/{id})
employee replaceemployee(@requestbody employee newemployee, @pathvariable long id) {
return repository.findbyid(id)
.map(employee -> {
employee.setname(newemployee.getname());
employee.setrole(newemployee.getrole());
return repository.save(employee);
})
.orelseget(() -> {
newemployee.setid(id);
return repository.save(newemployee);
});
}
@deletemapping(/employees/{id})
void deleteemployee(@pathvariable long id) {
repository.deletebyid(id);
}
}
@restcontroller表示每个方法返回的数据会直接写入响应体,而不是渲染模板。 anemployeerepository由构造函数注入到控制器中。 我们有每个操作的路由(@getmapping、@postmapping、@putmapping和@deletemapping,对应于 http get、post、put和delete调用)。(注意:阅读每种方法并了解它们的作用很有用。) employeenotfoundexception是用于指示何时查找但未找到员工的异常。 nonrest/src/main/java/payroll/employeenotfoundexception.java
package payroll;
class employeenotfoundexception extends runtimeexception {
employeenotfoundexception(long id) {
super(could not find employee + id);
}
}
当employeenotfoundexception抛出 an 时,spring mvc 配置的这个额外花絮用于呈现http 404:
nonrest/src/main/java/payroll/employeenotfoundadvice.java
package payroll;
import org.springframework.http.httpstatus;
import org.springframework.web.bind.annotation.controlleradvice;
import org.springframework.web.bind.annotation.exceptionhandler;
import org.springframework.web.bind.annotation.responsebody;
import org.springframework.web.bind.annotation.responsestatus;
@controlleradvice
class employeenotfoundadvice {
@responsebody
@exceptionhandler(employeenotfoundexception.class)
@responsestatus(httpstatus.not_found)
string employeenotfoundhandler(employeenotfoundexception ex) {
return ex.getmessage();
}
}
@responsebody表示此建议直接呈现到响应正文中。 @exceptionhandleremployeenotfoundexception将建议配置为仅在抛出an 时才响应。 @responsestatus说要发出一个httpstatus.not_found,即一个http 404。 建议的主体生成内容。在这种情况下,它会给出异常的消息。 要启动应用程序,请右键单击其中并从 idepublic static void main中payrollapplication选择运行,或者:
spring initializr 使用 maven 包装器,所以输入:
$ ./mvnw clean spring-boot:run 或者使用您安装的 maven 版本输入:
$ mvn clean spring-boot:run 当应用程序启动时,我们可以立即对其进行询。
$ curl -v localhost:8080/员工 这将产生:
* 尝试 ::1...* tcp_nodelay 设置* 连接到 localhost (::1) 端口 8080 (#0)> get /员工 http/1.1> 主机:本地主机:8080> 用户代理:curl/7.54.0> 接受:*/*>< http/1.1 200< 内容类型:application/json;charset=utf-8< 传输编码:分块< 日期:格林威治标准时间 2018 年 8 月 9 日星期四 17:58:00 获取 /employees/99 http/1.1> 主机:本地主机:8080> 用户代理:curl/7.54.0> 接受:*/*>< http/1.1 404< 内容类型: text/plain;charset=utf-8< 内容长度:26< 日期:格林威治标准时间 2018 年 8 月 9 日星期四 18:00:56<* 连接 #0 到主机 localhost 保持不变找不到员工 99 此消息很好地显示了http 404错误以及自定义消息could not find employee 99。
显示当前编码的交互并不难……
如果您使用 windows 命令提示符发出 curl 命令,则以下命令可能无法正常工作。您必须选择一个支持单引号参数的终端,或者使用双引号,然后转义 json 中的那些。
要创建新employee记录,我们在终端中使用以下命令——$开头的表示后面是终端命令:
$ curl -x post localhost:8080/employees -h 'content-type:application/json' -d '{name: samwise gamgee, role: gardener}' 然后它存储新创建的员工并将其发送回给我们:
{id:3,name:samwise gamgee,role:gardener} 您可以更新用户。让我们改变他的角色。
$ curl -x put localhost:8080/employees/3 -h 'content-type:application/json' -d '{name: samwise gamgee, role: ring bearer}' 我们可以看到输出中反映的变化。
{id:3,name:samwise gamgee,role:戒指持有者} 您构建服务的方式可能会产生重大影响。在这种情况下,我们说update,但replace是更好的描述。例如,如果未提供名称,则它将被取消。
最后,您可以像这样删除用户:
$ curl -x delete 本地主机:8080/employees/3# 现在如果我们再看一遍,它就不见了$ curl localhost:8080/employees/3找不到员工 3 这一切都很好,但是我们有 restful 服务了吗?(如果你没有听懂提示,答案是否定的。)
少了什么东西?
......未完待续......
2022就业季|spring认证教你,如何使用 spring 构建 rest 服务
#java##spring##spring认证##2022就业季#
以上就是今天关于spring的一些讨论,对你有帮助吗?如果你有兴趣深入了解,欢迎到spring中国教育管理中心留言交流!
Step7中如何实现PID控制,PID系统控制器的选择
苹果将投资4万亿韩元与起亚共同造车
我们说的是“曲奇云盘”
三星的七纳米是全球首个导入极紫外光微影光刻技术的晶圆代工厂
关于智能空调体感雷达方案的分析和应用
如何使用Spring构建REST服务(二)
汽车控制系统的创新 智能座舱集成域控制器
智慧工地的应用可进一步推动施工智能化管理和监控
骨科巨头们纷纷入局,“机器人大战”已经打响
中微半导亮相2023深圳国际电子展 为开发人员提供高效智能的解决方案
用LED灯替代汽车车灯的七大好处
一文读懂51单片机的RAM分区
苹果推送第三个iOS/iPadOS 14.5开发者测试版
我国成功打破西方的垄断,攻克9nm芯片技术难关
新能源电池种类
什么是复制字符串?Python如何复制字符串
Frost Sullivan:“Xilinx引领自动驾驶技术的未来”
诺基亚8本月16发布,真机曝光:你要的情怀配置都有,就是外观长这样要买4000?
美国造车新势力盯上中国市场,“水土不服”还是复现“特斯拉式”成功?
研究人员发现增强电化学微传感器性能的新方法,其竟还能解释物理学科