gpt4 book ai didi

java - Spring Boot REST Controller 问题

转载 作者:行者123 更新时间:2023-12-01 18:10:15 24 4
gpt4 key购买 nike

我在使用 Rest Controller 时遇到了一个非常奇怪的问题。我有一个非常基本的休息 Controller 。

package com.therealdanvega.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.therealdanvega.domain.Post;
import com.therealdanvega.service.PostService;

@RestController
public class PostController {

private PostService postService;

@Autowired
public PostController(PostService postService){
this.postService = postService;
}

@RequestMapping("posts/test")
public String test(){
return "test...";
}

@RequestMapping( name="/posts/", method=RequestMethod.GET )
public Iterable<Post> list(){
return postService.list();
}


}

调用服务

package com.therealdanvega.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.therealdanvega.domain.Post;
import com.therealdanvega.repository.PostRepository;

@Service
public class PostService {

private PostRepository postRepository;

@Autowired
public PostService(PostRepository postRepository){
this.postRepository = postRepository;
}

public Iterable<Post> list(){
return postRepository.findAll();
}

}

调用存储库来获取数据。

package com.therealdanvega.repository;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.therealdanvega.domain.Post;

@Repository
public interface PostRepository extends CrudRepository<Post, Long> {

Post findFirstByOrderByPostedOnDesc();

List<Post> findAllByOrderByPostedOnDesc();

Post findBySlug(String slug);

}

我正在内存数据库中使用 H2,并且其中只有一条 Post 记录,可以通过转到 H2 控制台并再次运行选择 Post 表来确认这一点。

如果我访问/test URL,我会得到我所期望的结果,即打印到浏览器的字符串“test...”。如果我尝试列出所有帖子(同样只有 1 个帖子),浏览器就会开始一遍又一遍地循环,并继续多次打印代表第 1 篇帖子的 JSON,以至于应用程序崩溃,我在控制台中看到了这一点

2015-11-07 17:58:42.959 ERROR 5546 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet dispatcherServlet threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response

这就是我访问/posts 时浏览器的样子,它应该只列出 1

enter image description here

Post Domain Class

package com.therealdanvega.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.springframework.data.annotation.CreatedDate;


@Entity
public class Post {

@Id @GeneratedValue
private Long id;
private String title;

@Column(columnDefinition = "TEXT")
private String body;

@Column(columnDefinition = "TEXT")
private String teaser;

private String slug;

@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
private Date postedOn;

@ManyToOne
private Author author;

@SuppressWarnings("unused")
private Post(){
}

public Post(String title){
this.setTitle(title);
}

// getters & setters
}

有谁知道我在这里做错或错过了什么?为什么不只显示 JSON 格式的 1 条记录?

最佳答案

您的 Post 对象似乎有循环引用。 Post 对象中的 Author 对象有一个 Posts 对象列表等。尝试将 @JsonIgnore 注释放在帖子对象的作者属性上。

您还可以使用@JsonBackReference@JsonManagedReference来解决问题。

来自Jackson documentation :

Object references, identity

@JsonManagedReference, @JsonBackReference: pair of annotations used toindicate and handle parent/child relationships expressed with pair ofmatching properties @JsonIdentityInfo: class/property annotation usedto indicate that Object Identity is to be used whenserializing/deserializing values, such that multiple references to asingle Java Object can be properly deserialized. This can be used toproperly deal with cyclic object graphs and directed-acyclic graphs.

关于java - Spring Boot REST Controller 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33588917/

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