如何根据映射 URL 从 RestController 获取实体

我有MyEntity类:


@Entity

@Table("entities)

public class MyEntity {


     @ID

     private String name;

     @Column(name="age")

     private int age;

     @Column(name="weight")

     private int weight;


     ...getters and setters..


}

在@RestController中有2 个@GetMapping方法。首先:


@GetMapping

public MyEntity get(){

   ...

   return myEntity;

第二:


@GetMapping("url")   

public List<MyEntity> getAll(){

   ...

   return entities;

}

需要提供:

1. @GetMapping返回实体,如MyEntity 类中所述。

2. @GetMapping("url")返回实体,就像它的字段之一是@JsonIgnore。


更新:


当我返回 myEntity 时,客户端将获得,例如:


{

"name":"Alex",

"age":30,

"weight":70

}

我希望在同一时间使用相同的 ENTITY有机会取决于发送给客户端的 URL:


1.


{

    "name":"Alex",

    "age":30,

    "weight":70

}

2.


{

    "name":"Alex",

    "age":30

    }


明月笑刀无情
浏览 113回答 3
3回答

慕后森

您还可以使用 JsonView Annotation 使其更清洁。定义视图public class View {&nbsp; &nbsp; static class Public { }&nbsp; &nbsp; static class ExtendedPublic extends Public { }&nbsp; &nbsp; static class Private extends ExtendedPublic { }}实体&nbsp; &nbsp; @Entity@Table("entities)public class MyEntity {&nbsp; &nbsp; &nbsp;@ID&nbsp; &nbsp; &nbsp;private String name;&nbsp; &nbsp; &nbsp;@Column(name="age")&nbsp; &nbsp; &nbsp;private int age;&nbsp; &nbsp; &nbsp;@JsonView(View.Private.class)&nbsp; &nbsp; &nbsp;@Column(name="weight")&nbsp; &nbsp; &nbsp;private int weight;&nbsp; &nbsp; &nbsp;...getters and setters..}在你的休息控制器中&nbsp; &nbsp; @JsonView(View.Private.class)&nbsp; &nbsp; @GetMapping&nbsp; &nbsp; public MyEntity get(){&nbsp; &nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; &nbsp; &nbsp;return myEntity;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; @JsonView(View.Public.class)&nbsp; &nbsp; @GetMapping("url")&nbsp; &nbsp;&nbsp; &nbsp; public List<MyEntity> getAll(){&nbsp; &nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; &nbsp; return entities;&nbsp; &nbsp; }

叮当猫咪

编辑:您可以将其序列化为 Map,而不是返回 Entity 对象,其中映射键表示属性名称。因此,您可以根据包含参数将值添加到地图中。@ResponseBodypublic Map<String, Object> getUser(@PathVariable("name") String name, String include) {&nbsp; &nbsp; User user = service.loadUser(name);&nbsp; &nbsp; // check the `include` parameter and create a map containing only the required attributes&nbsp; &nbsp; Map<String, Object> userMap = service.convertUserToMap(user, include);&nbsp; &nbsp; return userMap;}例如,如果您有这样的地图并且想要所有详细信息userMap.put("name", user.getName());userMap.put("age", user.getAge());userMap.put("weight", user.getWeight());现在如果你不想显示重量,那么你可以只放两个参数userMap.put("name", user.getName());userMap.put("age", user.getAge());

RISEBY

您可以创建两个 DTO 类,将您的实体转换为适当的 DTO 类并返回它。public class MyEntity {&nbsp; &nbsp; private String name;&nbsp; &nbsp; private int age;&nbsp; &nbsp; private int weight;&nbsp; &nbsp; public PersonDetailedDTO toPersonDetailedDTO() {&nbsp; &nbsp; &nbsp; &nbsp; PersonDetailedDTO person = PersonDetailedDTO();&nbsp; &nbsp; &nbsp; &nbsp; //...&nbsp; &nbsp; &nbsp; &nbsp; return person;&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; public PersonDTO toPersonDTO() {&nbsp; &nbsp; &nbsp; &nbsp; PersonDTO person = PersonDTO();&nbsp; &nbsp; &nbsp; &nbsp; //...&nbsp; &nbsp; &nbsp; &nbsp; return person;&nbsp;&nbsp;&nbsp; &nbsp; }}public class PersonDetailedDTO {&nbsp; &nbsp; private String name;&nbsp; &nbsp; private int age;&nbsp; &nbsp; private int weight;}public class PersonDTO {&nbsp; &nbsp; private String name;&nbsp; &nbsp; private int age;}@GetMappingpublic PersonDTO get() {&nbsp; &nbsp;//...&nbsp; &nbsp;return personService.getPerson().toPersonDTO();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java