简体   繁体   中英

How to create spring boot API with multiple "levels"

My problem is quite simple but I truly cannot figure out how to solve it.

I'm sorry I do not even know how those "levels" are actually called. Children? Tags? Also I am fairly new at this whole API REST and Spring Boot and JPA and such as I am currently doing an internship and am pretty much left on my own to do some tasks. But anyway.

What I would like my API to return as a response is:

<base>  
    <level1>
       <info1>
          <something1>{database data}</something1>
          <something2>{database data}</something2>
       </info1>
    </level1>
    <level2>
       <info2>
          <something3>{database data}</something3>
       </info2>
    </level2>
</base>

But right now all it returns with a GET request is this XML:

<something1>{database data}</something1>
<something2>{database data}</something2>
<something3>{database data}</something3>

The database data comes from a single table in my database that I get using JPA, and something1, something2, something3 are actually table columns.

I am using Spring Boot, JPA, Maven, Liquibase...
Is there any way I can achieve what I want? Getting level1, info1... If so, how?

This is how I get XML:

  • The repository is empty and just extends JPA:
@Repository
public interface AggregationDocumentRepository
    extends JpaRepository<AggregationDocumentEntity, Long> {
}
  • The service:
  public List<AggregationDocumentEntity> getAllAggregationDocuments() {
    return aggregationDocumentRepository.findAll();
  }
  • The controller:
  @GetMapping(value = "/aggregation", produces = {"application/json", "application/xml"})
  public List<AggregationDocumentEntity> getAllAggregationDocuments() {
    List<AggregationDocumentEntity> allAggregationDocuments =
        aggregationDocumentService.getAllAggregationDocuments();
    if (allAggregationDocuments.size() == 0) {
      log.error(
          "Cannot return the agreggation documents: there is no aggregation documents in the database.");
    } else {
      log.info("Returned all aggregation documents in the database.");
    }
    return allAggregationDocuments;
  }

I also have Entity with getters and setters of 16 values that are 16 columns of the database.
Everything works very well. The API response just needs to have those "levels".

Thank you in advance.

This isn't an API call, this is the response from an API call.

In effect, your response appears to be returning a single object with all of these attributes contained in it:

  • A list of levels
  • Info about each level

Your confusion comes in the fact that instead of returning a single object, you're returning the list of levels.

Ultimately how the data should be returned is a matter of personal taste and how you expect clients to interface with it, but this should at least get you on the right track.

So If I understand correctly you just want change the xml representational model, I use the Jackson dependency to solve that:

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>     
    </dependency>   

That's a sample Code:

@JacksonXmlRootElement(localName = "students")
@Data
public class StudentsRepresentationModel {
    
    @JsonProperty("student")
    @JacksonXmlElementWrapper(useWrapping = false) // Usado para
    @NonNull
    private List<StudentModel> students;

}

the XML with useWrapping = false:

<students>
    <student>
        <id>1</id>
        <name>John</name>
    </student>
    <student>
        <id>2</id>
        <name>Steven</name>
    </student>
<students>

the XML with useWrapping = true:

<students>
    <student>
        <student>
        <id>2</id>
        <name>Steven</name>
        </student>
    <student>
    <student>
        <student>
        <id>2</id>
        <name>Steven</name>
        </student>
    <student>
<students>

If you see in the Documentation you can make your Wrapper show or not the levels and change their names, in your case you are getting the methods implementation by the JpaRepository to the XML and listing them however if you disable that and create another Wrapper using the same Service you can make this work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM