常规的利用Curl发送json数据到后台SpringBoot+MongoDB测试CRUD

web后端的开发涉及很多很多技术,要相互配合好需要不断的练习。本文要做的事情是实现一个服务器端程序,运用SpringBoot技术,结合MongoDB数据库。然后用刚学的curl命令发送json数据来测试数据库的CRUD功能。

首先是新建一个工程并建立pom文件<?xml version=”1.0″ encoding=”UTF-8″?>`///2001/XMLSchema-instance”>com.hunter>0.0.1-SNAPSHOT>SpringBootMongo>>1.1.6.RELEASE>>org.springframework.boot>>spring-boot-starter-web>>>spring-boot-starter-actuator>>test>>com.hunter.Application>>>>

maven会帮助开发者进行依赖管理。然后开发者就可以专注于功能模块的开发。

在src/main/java/下新建目录com/hunter/,放入java文件

Application.java

package import orgimport orgimport orgimport org@Configuration@ComponentScan@EnableAutoConfigurationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

在com/hunter/employee目录下面新建三个文件 Employee.java

package com.hunter.employee;import org.springframework.data.annotation.Id;{@Idprivate String id;private String name;private String title;public Employee(){};public Employee(String id, String name, String title){super();this.id=id;this.name=name;this.title=title;}public String getId() {return id;}(String id) {this.id = id;}public String getName() {return name;}(String name) {this.name = name;}public String getTitle() {return title;}(String title) {this.title = title;}}

EmployeeController.java

package com.hunter.employee;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;@RestController@RequestMapping(“/employee”){@AutowiredEmployeeRepository repository;//create@RequestMapping(value = “”, method = RequestMethod.PUT)public Employee createEmployee(@RequestBody Employee employee){return repository.save(employee);}//read@RequestMapping(value = “”,method = RequestMethod.GET)public List<Employee> readEmployee(@RequestParam(required = false) String name,@RequestParam(required = false) String title) {if (name != null) {return repository.findByName(name);} else if (title != null) {return repository.findByTitle(title);} else {return repository.findAll();}}//update@RequestMapping(value = “/{id}”, method = RequestMethod.POST)public Employee updateEmployee(@PathVariable String id, @RequestBody Employee employee){Employee em = repository.findById(id);Employee newEm = new Employee(em.getId(),employee.getName(),employee.getTitle());return repository.save(newEm);}//delete@RequestMapping(value = “/{id}”, method = RequestMethod.DELETE)(@PathVariable String id) {repository.delete(id);}}

EmployeeRepository.java

package com.hunter.employee;import org.springframework.data.mongodb.repository.MongoRepository;import java.util.List;<<Employee> findByName(String name);public List<Employee> findByTitle(String title);public Employee findById(String id);}构建工程进行测试

一般我们都倾向于用ide去搭建工程以及测试,毕竟命令行做不到断点检查。 在终端cd到pom所在目录,可以直接用maven命令建立IDEA的工程 $ mvn idea:idea 然后就可以使用IntellijIDEA工作了。

接下来测试这个程序: 1. 打开MongoDB的接口。 $ mongod —dbpath ~/MongoDBPath 2. 运行程序 3. 终端输入 $ curl :8080/employee 会返回空数据 [] 因为现在数据库什么都没有。

$ curl -v -i -H “Accept: application/json” -H “Content-Type: application/json” -X PUT -d ‘{“id”:1,”name”:”charlie”,”title”:”here”}’ :8080/employee/

发送一个json数据包到服务器,如果运行正常,数据库已经写入一条记录了

$ curl -v -i -X GET :8080/employee 此时再次查询,就已经会返回一条关于charlie的数据了

$ curl -v -i -X POST -H “Accept: application/json” -H “Content-Type: application/json” -d ‘{“id”:1,”name”:”jane”,”title”:”leader”}’ :8080/employee/1

$ curl -v -i -X DELETE :8080/employee/1 删除id为1的数据,,也就是charlie没了。

强调,千万要注意引号的正确性。。。有时候,由于文本编辑工具的原因,引号会有误,也有可能造成curl命令错误。例如印象笔记就会把双引号变成另一种类型。。坑。。

总有一天,我会丢下我所有的疲倦和理想,

常规的利用Curl发送json数据到后台SpringBoot+MongoDB测试CRUD

相关文章:

你感兴趣的文章:

标签云: