Car*_*cas 15 java rest spring mongodb spring-boot
我正在使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
正如,hp并defenses显示为0和null分别.如果我删除@Transient标签,它就会通过.
只要你使用org.springframework.data.annotation.Transient它就应该按预期工作.杰克逊对弹簧数据一无所知,它忽略了它的注释.
示例代码,有效:
interface PersonRepository extends CrudRepository<Person, String> {}
Run Code Online (Sandbox Code Playgroud)
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()
}
Run Code Online (Sandbox Code Playgroud)
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
执行POST http://localhost:8080/person:
{
"name":"John Doe",
"age": 40
}
Run Code Online (Sandbox Code Playgroud)
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}]我通过使用@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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7194 次 |
| 最近记录: |