简体   繁体   English

如何在 Spring Boot 中返回模型中不存在的字段?

[英]How to return fields in Spring Boot that is not present in the model?

So I have a use case where I want to return a particular attribute for all the rows of a table.所以我有一个用例,我想为表的所有行返回一个特定的属性。 But the attribute does not exist in the table.但是该属性在表中不存在。 It should be generated on the fly.它应该是动态生成的。

For example - A table called Student - has the following attribute * ID * Name例如 - 一个名为Student的表 - 具有以下属性 * ID * Name

Now, when I use the repository for this model class, I want to return all the rows of data including ID and Name but also append another property called URL along with each row.现在,当我使用这个模型类的存储库时,我想返回所有数据行,包括IDName但还要附加另一个名为URL属性以及每一行。 So output will be -所以输出将是 -

  • ID ID
  • Name姓名
  • URL网址

The data for the URL can be generated on the fly - say = "example.com/"+ID URL的数据可以即时生成 - 例如 = "example.com/"+ID

How shall I proceed ?我将如何进行?

The same model/Repository/Service class for this entity are as follows -此实体的相同模型/存储库/服务类如下 -

Entity Student实体学生

@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class StudentDetail {

            @Id
            private Long ID;
            private String Name; 

    }

Repository存储库

@Repository
public interface StudentDetail  extends JpaRepository<StudentDetail , Integer> {

}

Service服务

@Override
    public List<StudentDetail> getStudentDetails() {
        List<StudentDetail> studentDetail = studentDetailsRepository.findAll();
        return studentDetail;
    }

The service will return all the rows with only ID and Name attribute.该服务将返回所有只有IDName属性的行。 How do I calculate the URL parameter in repository ?如何计算存储库中的 URL 参数?

You can add a transient field (it means, it won't be mapped to any column and persisted):您可以添加一个瞬态字段(这意味着它不会被映射到任何列并持久化):

public class StudentDetail {

        @Id
        private Long ID;
        private String Name;
        @Transient
        private String url = // Here comes your implementation

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在spring boot中仅从api返回模型的特定字段 - Return only specific fields of model from api in spring boot 如何在模型验证 Spring Boot 中返回 400 状态 - How to return 400 status in model validation spring boot Spring Boot 从模型中提取有限的字段 - Spring boot extracting limited fields from a model Spring 引导:控制每个请求的序列化响应中是否存在 null 字段 - Spring Boot: Control if null fields present in serialized response per request GraphQL Spring 引导 Web 拦截器返回错误响应,如果标头不存在 - GraphQL Spring Boot Web Interceptor to return an error response if headers not present 如何从另一个新的 Spring 启动项目中调用 Spring 启动项目中的 Spring 启动 api - How to call a Spring boot api present in one Spring boot project from another new Spring boot project 如何使用Spring Boot从自定义查询返回包含少量字段的自定义列表? - How to return a custom List with few fields from a custom query with Spring Boot? 如何使用 elasticsearch 和 spring-boot (springframework.data.elasticsearch) 返回距离和所有字段 - How to return distance and all fields using elasticsearch and spring-boot (springframework.data.elasticsearch) 将List-of-List Spring-Boot模型字段与Thymeleaf相关联 - Relating List-of-List Spring-Boot Model Fields to Thymeleaf 如何按 Java Spring Boot 中的字段对列表进行排序? - How to sort the list by fields in Java Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM