我有以下问题,我需要使用springboot应用程序中的save方法在elasticsearch中保存一个hateos结构,但是我得到了错误消息“stackoverflowerrror null”:有人能检查一下我的代码,看看我在哪里出错吗?我花了很长时间来演示这个例子。
配置.java
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.example.demo.elsticsearch")
public class Config {
@Bean
public RestHighLevelClient client() {
ClientConfiguration clientConfiguration
= ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchRestTemplate(client());
}
}
userresourceassembler.java
@Service
public class UserResourseAssembler extends RepresentationModelAssemblerSupport<User, UserResourse> {
public UserResourseAssembler() {
super(JsController.class, UserResourse.class);
}
@Override
public UserResourse toModel(User user) {
UserResourse resource = UserResourse.builder().email("jjj").firstName("hii").lastName("hoo").loginId("123").build();
resource.add(linkTo(methodOn(JsController.class).getByLoginId(user.getLoginId())).withRel("self"));
return resource;
}
}
回购.java
@Repository
public interface Repo extends ElasticsearchRepository<UserResourse, String> {
List<UserResourse> findByLoginId(String loginId);
}
用户.java
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String loginId;
private String firstName;
private String lastName;
private String email;
}
用户资源.java
@Mapping(mappingPath = "/files.json")
@Document(indexName = "files", type = "file")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UserResourse extends RepresentationModel<UserResourse> {
@Id
private String loginId;
private String firstName;
private String lastName;
private String email;
}
jscontroller.java文件
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class JsController {
private final Repo repository;
private final UserResourseAssembler userResourseAssembler;
@GetMapping("/check")
public ResponseEntity<UserResourse> uploadData() {
User user = new User(UUID.randomUUID().toString(), "hi", "hello", "jj");
UserResourse userResourse = userResourseAssembler.toModel(user);
repository.save(userResourse);
return new ResponseEntity<>(userResourse, HttpStatus.OK);
}
@GetMapping("/jobId")
public List<UserResourse> getByLoginId(@PathVariable String loginId) {
return repository.findByLoginId(loginId);
}
}
我得到以下错误:
2020-09-18 10:02:35.939 ERROR 16184 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.elasticsearch.ElasticsearchException: Cannot render document to JSON] with root cause
java.lang.StackOverflowError: null
at java.base/java.lang.ClassLoader.defineClass1(Native Method) ~[na:na]
暂无答案!
目前还没有任何答案,快来回答吧!