gpt4 book ai didi

java - Spring Boot 中的 com.mongodb.util.JSONParseException?

转载 作者:行者123 更新时间:2023-11-30 02:18:15 26 4
gpt4 key购买 nike

我尝试使用 @Query 注释创建自定义查询。但它会引发一些错误。

这是我的存储库

package com.springboot.springmongodb.user;
import java.util.List;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

public interface UserRepository extends CrudRepository<User, String>{
@Override
User findOne(String id);

@Transactional(readOnly=false)
@Query("SELECT user FROM User user WHERE user.username = :username")
public List<User> findByUsername(@Param("username") String username);

}

我想创建自定义查询来检查用户名。

这是我收到的错误。

2017-12-04 14:46:10.413 WARN 1452 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDetailsService': Unsatisfied dependency expressed through field 'userRep'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is com.mongodb.util.JSONParseException: SELECT user FROM User user WHERE user.username = :username ^

最佳答案

异常表明 @Query你给出的结果被视为 JSON。通过查看您的导入,我们可以得出原因:

import org.springframework.data.mongodb.repository.Query; 

而您的查询SELECT user FROM User user WHERE user.username = :username对于 JPA (SQL) 有效,对于 MongoDB 无效。更改导入以适合您的数据库或更改查询以适合它。

有效的 mongoDB 看起来像 Spring Data MongoDB example :

public interface PersonRepository extends MongoRepository<Person, String>

@Query("{ 'username' : ?0 }")
List<Person> findByThePersonsFirstname(String firstname);

}

...以及 Spring Data JPA example 中的有效 JPA 语句:

public interface UserRepository extends JpaRepository<User, Long> {

@Query("select u from User u where u.username = ?1")
User findByEmailAddress(String emailAddress);
}

这需要导入 org.springframework.data.jpa.repository.Query

关于java - Spring Boot 中的 com.mongodb.util.JSONParseException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47629860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com