我使用spring-boot来提供与MongoDB持久存在的REST接口。我正在使用'标准'依赖性来支持它,包括spring-boot-starter-data-mongodb
和spring-boot-starter-web
。
但是,在我的一些类中,我有一些注释@Transient
的字段,以便MongoDB不会保留该信息。但是,我希望这些信息在我的休息服务中发送出去。不幸的是,MongoDB和其他控制器似乎都共享该注释。因此,当我的前端收到JSON对象时,这些字段不会被实例化(但仍然被声明)。删除注释允许字段在JSON对象中通过。
我如何分别为MongoDB和REST配置瞬态?
这是我的班级
package com.clashalytics.domain.building;
import com.clashalytics.domain.building.constants.BuildingConstants;
import com.clashalytics.domain.building.constants.BuildingType;
import com.google.common.base.Objects;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import java.util.*;
public class Building {
@Id
private int id;
private BuildingType buildingType;
private int level;
private Location location;
// TODO http://stackoverflow.com/questions/30970717/specify-field-is-transient-for-mongodb-but-not-for-restcontroller
@Transient
private int hp;
@Transient
private BuildingDefense defenses;
private static Map<Building,Building> buildings = new HashMap<>();
public Building(){}
public Building(BuildingType buildingType, int level){
this.buildingType = buildingType;
this.level = level;
if(BuildingConstants.hpMap.containsKey(buildingType))
this.hp = BuildingConstants.hpMap.get(buildingType).get(level - 1);
this.defenses = BuildingDefense.get(buildingType, level);
}
public static Building get(BuildingType townHall, int level) {
Building newCandidate = new Building(townHall,level);
if (buildings.containsKey(newCandidate)){
return buildings.get(newCandidate);
}
buildings.put(newCandidate,newCandidate);
return newCandidate;
}
public int getId() {
return id;
}
public String getName(){
return buildingType.getName();
}
public BuildingType getBuildingType() {
return buildingType;
}
public int getHp() {
return hp;
}
public int getLevel() {
return level;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public BuildingDefense getDefenses() {
return defenses;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Building building = (Building) o;
return Objects.equal(id, building.id) &&
Objects.equal(hp, building.hp) &&
Objects.equal(level, building.level) &&
Objects.equal(buildingType, building.buildingType) &&
Objects.equal(defenses, building.defenses) &&
Objects.equal(location, building.location);
}
@Override
public int hashCode() {
return Objects.hashCode(id, buildingType, hp, level, defenses, location);
}
}
原样,hp
和defenses
分别显示为0
和null
。如果我删除@Transient
标记,则会显示。
答案 0 :(得分:5)
只要您使用org.springframework.data.annotation.Transient
,它就应该按预期工作。杰克逊对 spring-data 一无所知,它忽略了它的注释。
示例代码,有效:
interface PersonRepository extends CrudRepository<Person, String> {}
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
class Person {
@Id
private String id;
private String name;
@Transient
private Integer age;
// setters & getters & toString()
}
@RestController
@RequestMapping("/person")
class PersonController {
private static final Logger LOG = LoggerFactory.getLogger(PersonController.class);
private final PersonRepository personRepository;
@Autowired
PersonController(PersonRepository personRepository) {
this.personRepository = personRepository;
}
@RequestMapping(method = RequestMethod.POST)
public void post(@RequestBody Person person) {
// logging to show that json deserialization works
LOG.info("Saving person: {}", person);
personRepository.save(person);
}
@RequestMapping(method = RequestMethod.GET)
public Iterable<Person> list() {
Iterable<Person> list = personRepository.findAll();
// setting age to show that json serialization works
list.forEach(foobar -> foobar.setAge(18));
return list;
}
}
执行 POST http://localhost:8080/person
:
{
"name":"John Doe",
"age": 40
}
Saving person: Person{age=40, id='null', name='John Doe'}
person
集合中的条目:
{ "_id" : ObjectId("55886dae5ca42c52f22a9af3"), "_class" : "demo.Person", "name" : "John Doe" }
- 年龄未持久执行 GET http://localhost:8080/person
:
[{"id":"55886dae5ca42c52f22a9af3","name":"John Doe","age":18}]
答案 1 :(得分:4)
你的问题似乎是mongo和jackson都表现得像预期的那样。 Mongo不会保留数据,而jackson会忽略该属性,因为它被标记为瞬态。我设法通过&#39;欺骗&#39; jackson忽略瞬态字段,然后用@JsonProperty
注释getter方法。这是我的样本bean。
@Entity
public class User {
@Id
private Integer id;
@Column
private String username;
@JsonIgnore
@Transient
private String password;
@JsonProperty("password")
public String getPassword() {
return // your logic here;
}
}
这不仅仅是一个合适的解决方案,所以我不确定这是否会为您带来任何副作用。
答案 2 :(得分:2)
卡洛斯-bribiescas, 你用的是什么版本的。它可能是版本问题。因为这个瞬态注释只是为了不坚持到mongo db。请尝试更改版本。可能类似于Maciej one(1.2.4版本)
在其中一个版本中,有一个json解析spring数据项目的问题。 http://www.widecodes.com/CyVjgkqPXX/fields-with-jsonproperty-are-ignored-on-deserialization-in-spring-boot-spring-data-rest.html
答案 3 :(得分:2)
由于您没有将MongoRepositories
作为具有Spring Data REST的平静端点公开,因此让您的Resources/endpoint
响应与您的域模型分离更有意义,这样您的域模型就可以在没有影响您的其他客户/消费者。对于资源,您可以考虑利用Spring HATEOAS提供的内容。
答案 4 :(得分:2)
我使用 @JsonSerialize 解决了这个问题。如果您希望将其除去,也可以选择 @JsonDeserialize 。
@Entity
public class Article {
@Column(name = "title")
private String title;
@Transient
@JsonSerialize
@JsonDeserialize
private Boolean testing;
}
// No annotations needed here
public Boolean getTesting() {
return testing;
}
public void setTesting(Boolean testing) {
this.testing = testing;
}
答案 5 :(得分:0)
我通过实现自定义JacksonAnnotationIntrospector
解决了这个问题:
@Bean
@Primary
ObjectMapper objectMapper() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
AnnotationIntrospector annotationIntrospector = new JacksonAnnotationIntrospector() {
@Override
protected boolean _isIgnorable(Annotated a) {
boolean ignorable = super._isIgnorable(a);
if (ignorable) {
Transient aTransient = a.getAnnotation(Transient.class);
JsonIgnore jsonIgnore = a.getAnnotation(JsonIgnore.class);
return aTransient == null || jsonIgnore != null && jsonIgnore.value();
}
return false;
}
};
builder.annotationIntrospector(annotationIntrospector);
return builder.build();
}
此代码对org.springframework.data.annotation.Transient
进行了不可见的Jackson
注释,但对mongodb
有效。