postman初体验 以及@RequestBody

@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接口。

三、测试

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

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值