首页 文章

运行时的Couchbase查询异常key的不支持的参数类型:class com.couchbase.client.protocol.views.Query

提问于
浏览
0

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

我在Query类的setKey()方法上设置了一个字符串,得到了一个异常 . 但后来我检查了API并提供了一个json到setKey,仍然没有工作 . 已经搜索了很多,但无法让它工作 .

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

Application.properties

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

PlayerRepository

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);

}

Player.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 class

@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);
    }

}

}

PlayerService class

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);
}

1 回答

  • 0

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

    您可以使用 CouchbaseTemplate 来执行手动查询 .

相关问题