0

每次我尝试从我的 Spring Boot 应用程序中查询 Couchbase DB 上的视图时,都会遇到此异常。不支持的键参数类型:com.couchbase.client.protocol.views.Query 类。

我在 Query 类的 setKey() 方法上设置了一个字符串,但出现异常。但后来我检查了 API 并提供了一个 json 给 setKey,仍然无法正常工作。进行了很多搜索,但无法使其正常工作。

我也在这篇文章中分享代码片段。

应用程序属性

spring.couchbase.bucket.password=
spring.couchbase.bucket.name=default
spring.couchbase.bootstrap-hosts=127.0.0.1
spring.data.couchbase.repositories.enabled=true

播放器存储库

public interface PlayerRepository extends CouchbaseRepository<Player, Integer>
{

    @View(designDocument = "player", viewName = "all")
    public List<Player> findAll();
    @View(designDocument = "player", viewName = "by_Name")
    public Player findByName(Query query);
    @View(designDocument = "player", viewName = "by_TeamId")
    public List<Player> findByTeamId(Query query);

}

播放器.java

@Document
public class Player
{
    @Id
    int playerId;
    @Field
    String name;
    @Field
    String type;
    @Field
    String country;
    @Field
    String playingHand;
    @Field
    String era;
    @Field
    int teamId;
    @Field
    int odiCenturies;
    @Field
    int testCenturies;

    public Player(){}


    public Player(int playerId, String name, String type, String country, String playingHand, String era, int teamId,
            int odiCenturies, int testCenturies) {
        super();
        this.playerId = playerId;
        this.name = name;
        this.type = type;
        this.country = country;
        this.playingHand = playingHand;
        this.era = era;
        this.teamId = teamId;
        this.odiCenturies = odiCenturies;
        this.testCenturies = testCenturies;
    }

SpringBootApplication 类

@SpringBootApplication 公共类 CricketTeamSelectionMain {

/**
 * @param args
 */
public static void main(String[] args) 
{
    SpringApplication.run(CricketTeamSelectionMain.class, args);

}

@Configuration
@EnableCouchbaseRepositories
public static class DBConfig extends AbstractCouchbaseConfiguration
{

    @Value("${spring.couchbase.bucket.name}")
    private String bucketName;

    @Value("${spring.couchbase.bucket.password}")
    private String password;

    @Value("${spring.couchbase.bootstrap-hosts}")
    private String ip;

    @Override
    public String getBucketName() {
        return this.bucketName;
    }

    @Override
    public String getBucketPassword() {
        return this.password;
    }

    @Override
    public List<String> getBootstrapHosts() {
        return Arrays.asList(this.ip);
    }

}

}

播放器服务类

package org.ups.fantasyCricket.service;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ups.fantasyCricket.CricketTeamSelectionMain.DBConfig;
import org.ups.fantasyCricket.Model.Player;
import org.ups.fantasyCricket.Repository.PlayerRepository;

import com.couchbase.client.CouchbaseClient;
import com.couchbase.client.protocol.views.Query;
import com.couchbase.client.protocol.views.View;
import com.couchbase.client.protocol.views.ViewResponse;

@Service
public class PlayerService 
{

    @Autowired
    PlayerRepository playerRepo;

    private CouchbaseClient client;


    public List<Player> getAllPlayers() 
    {
        List<Player> allPlayerLists = new ArrayList<Player>();
        /*allPlayerLists.addAll((Collection<? extends Player>) playerRepo.findAll());
        return allPlayerLists;*/
        playerRepo.findAll().forEach(allPlayerLists::add);
        return allPlayerLists;
    }

    public Player getPlayerByName(String name) 
    {
        DBConfig dbCon = new DBConfig();

        try
        {
            Query query = new Query();
            query.setIncludeDocs(true);
            query.setKey(name);
            Player player = playerRepo.findByName(query);
            return player;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return null;

    }

    public String addPlayer(Player player) 
    {
        playerRepo.save(player);
        return "Success";
    }

    public String updatePlayer(Player player, int id)
    {
        playerRepo.save(player);
        return "Success";
    }

    public List<Player> getPlayersByTeamId(int teamId) 
    {
        List<Player> allPlayerLists = new ArrayList<Player>();
        Query query = new Query();
        query.setKey(String.valueOf(teamId));
        playerRepo.findByTeamId(query).forEach(allPlayerLists::add);
        return allPlayerLists;

    }

    public String addPlayers(List<Player> players)
    {
        playerRepo.save(players);
        return "Success";
    }
}

在 CouchBase DB 上查看 by_Name

function (doc) {
emit(doc.name, doc);
}
4

1 回答 1

0

您使用的是哪个版本的 spring-data-couchbase?从 2.x 开始,@Query注释使用查询派生,您不能再将 aViewQuery作为参数使用...查看文档,了解查询派生与视图。

不过,您可能可以使用CouchbaseTemplate来执行手动查询。

于 2017-02-14T20:00:34.433 回答