【问题标题】:Sprint Date Rest successful, but no dataSprint Date Rest 成功,但没有数据
【发布时间】:2015-05-18 14:40:40
【问题描述】:

实体

@Data
@Accessors(chain = true, fluent = true)
@Entity
@Table(name = "T_NOTE")
@Access(AccessType.FIELD)
public class Note implements Serializable
{
    @Id
    @GeneratedValue
    private Long id;

    private Date date;
    @Column(length = 2000)
    private String content;
    private String title;
    private String weather;
}

存储库

@RepositoryRestResource(collectionResourceRel = "note", path = "note")
public interface NoteRepository extends AbstractRepository<Note, Long>
{

}

获取http://localhost:8080/note/2

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/note/2"
        }
    }
}

没有实体字段数据,为什么?

EIDT

添加标准 setter/getter 后,现在一切正常。

public Long getId()
{
    return id;
}

public void setId(Long id)
{
    this.id = id;
}

public Date getDate()
{
    return date;
}

public void setDate(Date date)
{
    this.date = date;
}

public String getContent()
{
    return content;
}

public void setContent(String content)
{
    this.content = content;
}

public String getTitle()
{
    return title;
}

public void setTitle(String title)
{
    this.title = title;
}

public String getWeather()
{
    return weather;
}

public void setWeather(String weather)
{
    this.weather = weather;
}

这是由 jackson mapper 造成的吗?我该如何使用 fluent API?为什么不直接使用反射来生成 JSON?

编辑

我需要的是这个配置

@Configuration
@Import(RepositoryRestMvcConfiguration.class)
public class ShoweaRestMvcConfiguration extends RepositoryRestMvcConfiguration
{
    @Override
    protected void configureJacksonObjectMapper(ObjectMapper mapper)
    {
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
        mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    }
}

this引起

【问题讨论】:

  • 如果你删除@Accessors,有什么不同吗?

标签: spring spring-mvc jpa spring-data spring-data-rest


【解决方案1】:

@Accessors 可能会越过@Data 注释,并使用fluent = true 生成与字段同名的getter,例如id()date() (@Accessor documentation)。这就是 Spring 看不到任何字段的原因。

我认为您可以安全地删除@Accessors@Access,因为@Access 的默认值来自id(如果您注释了该字段,它将是FIELD,如果您注释了getter,它将是PROPERTY)。

【讨论】:

  • 是的,它有效,我只是添加了 setter/getter 并没有改变流畅的访问器。更新了问题,但是为什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
相关资源
最近更新 更多