Sprint Date Rest成功,但没有数据

时间:2015-03-16 13:05:39

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

实体

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

这是杰克逊映射器的原因吗?我如何使用流畅的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

引起

1 个答案:

答案 0 :(得分:1)

@Accessors可能会踩过@Data注释,而fluent = true会生成与字段名称相同的getter,例如id()date()@Accessor documentation)。这就是为什么Spring没有看到任何领域的原因。

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