postman初体验 以及@RequestBody

    科技2022-07-21  122

    @RequestBody用于在请求体中获取参数,一般为json或者xml格式的数据。

    本文示例使用postman请求保存数据接口,将json格式的数据保存到elasticsearch中。

    目录

    一、编写controller接口

    二、postman发送请求

    三、测试


    一、编写controller接口

    package cn.jack.elasticsearchdemo.controller; import cn.jack.elasticsearchdemo.domain.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.GetQuery; import org.springframework.data.elasticsearch.core.query.IndexQuery; import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/op") public class OperationController { @Autowired private ElasticsearchOperations elasticsearchOperations; /** * 保存数据,Content-Type为application/json的数据,需要使用@RequestBody注解接收 * @return */ @PostMapping("/person") public String save(@RequestBody Person person) { IndexQuery indexQuery = new IndexQueryBuilder() .withId(person.getId()) .withObject(person) .build(); IndexCoordinates indexCoordinates = IndexCoordinates.of("jack_person"); String documentId = elasticsearchOperations.index(indexQuery, indexCoordinates); return documentId; } /** * 通过id查找Person数据 * @param id * @return */ @GetMapping("/person/{id}") public Person findById(@PathVariable("id") String id) { Person person = elasticsearchOperations .queryForObject(GetQuery.getById(id), Person.class); return person; } }

    二、postman发送请求

    启动应用,postman请求save接口。

    三、测试

    访问查询接口,确认数据保存成功。

     

     

     

     

     

     

     

     

     

    Processed: 0.010, SQL: 8