如何使用Spring Boot正确实现向Elasticsearch添加新记录?

问题描述 投票:0回答:1

Spring Boot / Spring Data / Elasticsearch应用程序入门。 ES - > 6.1

有一个简单的存储库:

public interface BusinessMetadataRepository extends ElasticsearchRepository<BusinessMetadata, Long> {
    List<BusinessMetadata> findByName(String name);

    List<BusinessMetadata> findById(Long id);

}

和业务对象:

import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "bsn", type = "mtd", shards = 1)
public class BusinessMetadata {

    private Long id;
    private String name;

    public Long getId() {return id;}

    public void setId(Long id) {this.id = id;}

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BusinessMetadata(Long id, String name) {

        this.id = id;
        this.name = name;
    }

    public BusinessMetadata() {

    }
}

弹性配置:

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.discover.harmony.elastic.repository")
public class ElasticConfiguration {


    @Bean
    public NodeBuilder nodeBuilder() {
        return new NodeBuilder();
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() throws IOException {
        File tmpDir = File.createTempFile("elastic", Long.toString(System.nanoTime()));
        System.out.println("Temp directory: " + tmpDir.getAbsolutePath());
        Settings.Builder elasticsearchSettings =
                Settings.settingsBuilder()
                        .put("http.enabled", "true") // 1
                        .put("index.number_of_shards", "1")
                        .put("path.data", new File(tmpDir, "data").getAbsolutePath()) // 2
                        .put("path.logs", new File(tmpDir, "logs").getAbsolutePath()) // 2
                        .put("path.work", new File(tmpDir, "work").getAbsolutePath()) // 2
                        .put("path.home", tmpDir); // 3



        return new ElasticsearchTemplate(nodeBuilder()
                .local(true)
                .settings(elasticsearchSettings.build())
                .node()
                .client());
    }
}

我的Rest控制器用于搜索工作正常:

@RestController
@RequestMapping("/rest/search")
public class SearchResource {

    @Autowired
    BusinessMetadataRepository businessMetadataRepository;

    @GetMapping(value = "/name/{text}")
    public List<BusinessMetadata> searchName(@PathVariable final String text) {
        return businessMetadataRepository.findByName(text);
    }

    @GetMapping(value = "/all")
    public List<BusinessMetadata> searchAll() {
        List<BusinessMetadata> businessMetadataList = new ArrayList<>();
        Iterable<BusinessMetadata> businessMetadata = businessMetadataRepository.findAll();
        businessMetadata.forEach(businessMetadataList::add);
        return businessMetadataList;
    }


}

我的Rest Controller用于保存:

@RestController
@RequestMapping("/rest/save")
public class SaveResource {

    @Autowired
    BusinessMetadataRepository businessMetadataRepository;

    @GetMapping(value = "/name/{text}")
    public void Save(String text) {
        businessMetadataRepository.save(new BusinessMetadata((long)99, text));
    }
}

当我使用Postman测试保存时,我收到此错误:

{
    "timestamp": 1514325625996,
    "status": 405,
    "error": "Method Not Allowed",
    "exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
    "message": "Request method 'POST' not supported",
    "path": "/rest/save/name/new-1"
}

为了正确配置此项目以支持插入新文档,我需要进行哪些更改?

elasticsearch spring-boot
1个回答
0
投票

根据AntJavaDev的评论,我用以下方式修改了我的控制器:

@RestController
@RequestMapping("/rest/save")
public class SaveResource {

    @Autowired
    BusinessMetadataRepository businessMetadataRepository;

    @PostMapping("/name/{text}")
    public void Save(@PathVariable String text) {
        BusinessMetadata mtd = businessMetadataRepository.save(new BusinessMetadata(text));
    }
}

2个关键更改是:将@GetMapping替换为@PostMapping,并将@PathVariable包含为参数限定符。

现在它按预期工作

© www.soinside.com 2019 - 2024. All rights reserved.