Okay, here is the Spring Boot endpoint with the requested structure and mock data populated using setters.
Since the provided class definitions only contained fields, I've added no-argument constructors and public setters to allow the mock data to be set. I've also added dummy classes for DokumentData and DokumentDetailAnsichtData as their definitions were missing but referenced.
1. Add Setters and Constructors to your Classes:
You need to add a no-argument constructor and public setters for the fields you want to populate in your data classes.
import java.io.Serializable;
import java.time.LocalDateTime;
// Dummy classes for fields not defined in the request
class DokumentData {}
class DokumentDetailAnsichtData {}
// Assuming Kategorie is a String or Enum. Using String for mock simplicity.
// enum Kategorie { STANDARD, EXPRESS }
public class AusgabeData {
private DokumentData dokumentData;
private long logId;
private LocalDateTime anforderungsDatum;
private String anfordererName;
private String anfordererUserId;
private String raum;
// No-argument constructor is good practice for beans/POJOs
public AusgabeData() {}
// Setters
public void setDokumentData(DokumentData dokumentData) { this.dokumentData = dokumentData; }
public void setLogId(long logId) { this.logId = logId; }
public void setAnforderungsDatum(LocalDateTime anforderungsDatum) { this.anforderungsDatum = anforderungsDatum; }
public void setAnfordererName(String anfordererName) { this.anfordererName = anfordererName; }
public void setAnfordererUserId(String anfordererUserId) { this.anfordererUserId = anfordererUserId; }
public void setRaum(String raum) { this.raum = raum; }
// Optional: Add Getters if needed elsewhere, but not strictly for this mock endpoint
public DokumentData getDokumentData() { return dokumentData; }
public long getLogId() { return logId; }
public LocalDateTime getAnforderungsDatum() { return anforderungsDatum; }
public String getAnfordererName() { return anfordererName; }
public String getAnfordererUserId() { return anfordererUserId; }
public String getRaum() { return raum; }
}
import java.io.Serializable;
// Dummy classes... (already defined above, just noting they are needed)
// class DokumentData {}
// class DokumentDetailAnsichtData {}
// Assuming Kategorie is a String or Enum. Using String for mock simplicity.
// enum Kategorie { STANDARD, EXPRESS }
public class VersandDataAndServiceProvider implements Serializable {
private String barcode;
private String userId; // Note: This is 'userId', possibly distinct from anfordererUserId
private String kategorie; // Using String for mock simplicity
// private AusgabeData data; // <-- This field's usage is unclear in the AnforderungssichtDataElement context. Ignoring it for mock population.
private DokumentDetailAnsichtData detailData;
private boolean nichtVorhanden = false;
// No-argument constructor
public VersandDataAndServiceProvider() {}
// Setters
public void setBarcode(String barcode) { this.barcode = barcode; }
public void setUserId(String userId) { this.userId = userId; }
public void setKategorie(String kategorie) { this.kategorie = kategorie; }
// public void setData(AusgabeData data) { this.data = data; } // Not setting this internal data field
public void setDetailData(DokumentDetailAnsichtData detailData) { this.detailData = detailData; }
public void setNichtVorhanden(boolean nichtVorhanden) { this.nichtVorhanden = nichtVorhanden; }
// Optional: Add Getters
public String getBarcode() { return barcode; }
public String getUserId() { return userId; }
public String getKategorie() { return kategorie; }
// public AusgabeData getData() { return data; }
public DokumentDetailAnsichtData getDetailData() { return detailData; }
public boolean isNichtVorhanden() { return nichtVorhanden; }
}
// Dummy classes... (already defined above, just noting they are needed)
// class AusgabeData {}
// class VersandDataAndServiceProvider {}
public class AnforderungssichtDataElement {
private AusgabeData data; // This is the primary AusgabeData for the element
private VersandDataAndServiceProvider provider;
// No-argument constructor
public AnforderungssichtDataElement() {}
// Setters
public void setData(AusgabeData data) { this.data = data; }
public void setProvider(VersandDataAndServiceProvider provider) { this.provider = provider; }
// Optional: Add Getters
public AusgabeData getData() { return data; }
public VersandDataAndServiceProvider getProvider() { return provider; }
}
2. Create the Spring Boot Controller Endpoint:
This controller will create instances of your data classes and populate them using the setters before returning a list containing these populated objects.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections; // Used for returning a single element list
// Assuming your data classes (AusgabeData, AnforderungssichtDataElement, VersandDataAndServiceProvider)
// are in the same package or imported correctly.
// Also assuming the dummy classes DokumentData and DokumentDetailAnsichtData exist.
@RestController
public class AnforderungssichtController {
@Operation(summary = "Dokumente zu einer Suchanfrage")
@GetMapping(path = "/anforderungssicht")
public List<AnforderungssichtDataElement> anforderungssicht() {
// --- Create Mock Data Element 1 ---
AnforderungssichtDataElement element1 = new AnforderungssichtDataElement();
// Create and populate AusgabeData
AusgabeData ausgabeData1 = new AusgabeData();
ausgabeData1.setLogId(1001L);
ausgabeData1.setAnforderungsDatum(LocalDateTime.now().minusDays(2).minusHours(5));
ausgabeData1.setAnfordererName("Dr. Anna Schmidt");
ausgabeData1.setAnfordererUserId("aschmidt");
ausgabeData1.setRaum("Archiv R10");
ausgabeData1.setDokumentData(null); // Set complex objects to null or mock them
// Create and populate VersandDataAndServiceProvider
VersandDataAndServiceProvider provider1 = new VersandDataAndServiceProvider();
provider1.setBarcode("DOCBARCODE001");
provider1.setUserId("svc_prov_a");
provider1.setKategorie("STANDARD"); // Using String mock
provider1.setNichtVorhanden(false);
provider1.setDetailData(null); // Set complex objects to null or mock them
// provider1.setData(...) - Ignoring the internal data field
// Set the populated objects into the AnforderungssichtDataElement
element1.setData(ausgabeData1);
element1.setProvider(provider1);
// --- Create Mock Data Element 2 (Optional - demonstrating multiple rows) ---
AnforderungssichtDataElement element2 = new AnforderungssichtDataElement();
AusgabeData ausgabeData2 = new AusgabeData();
ausgabeData2.setLogId(1002L);
ausgabeData2.setAnforderungsDatum(LocalDateTime.now().minusHours(3));
ausgabeData2.setAnfordererName("Peter Müller");
ausgabeData2.setAnfordererUserId("pmueller");
ausgabeData2.setRaum("Keller Lager");
ausgabeData2.setDokumentData(null);
VersandDataAndServiceProvider provider2 = new VersandDataAndServiceProvider();
provider2.setBarcode("DOCBARCODE002");
provider2.setUserId("svc_prov_b");
provider2.setKategorie("EXPRESS"); // Using String mock
provider2.setNichtVorhanden(true); // Example of a different value
provider2.setDetailData(null);
element2.setData(ausgabeData2);
element2.setProvider(provider2);
// --- Return the list ---
// Option 1: Return the list with multiple elements
List<AnforderungssichtDataElement> resultList = new ArrayList<>();
resultList.add(element1);
resultList.add(element2);
return resultList;
// Option 2: Return a list with only one element (as in your template)
// return Collections.singletonList(element1); // Or element2
// Option 3: If you want to return the list with just one element,
// you can also just create one element and add it:
// List<AnforderungssichtDataElement> resultList = new ArrayList<>();
// resultList.add(element1);
// return resultList;
}
}
Explanation:
- Setters and Constructors: I've added a default constructor (
public ClassName() {}) and public setters (setFieldName(...)) to each of your data classes. These are essential for creating instances and populating their fields programmatically in the controller.
- Dummy Classes:
DokumentData and DokumentDetailAnsichtData were referenced but not defined. I added simple empty classes for them so the code compiles. In a real application, you would use their actual definitions. Kategorie was assumed to be a String for simplicity in the mock.
- RestController: The
@RestController annotation marks the class as a web controller that returns data directly (usually JSON).
- GetMapping:
@GetMapping(path = "/anforderungssicht") maps HTTP GET requests to the /anforderungssicht path to this method.
- Operation:
@Operation(summary = "...") is for Swagger/OpenAPI documentation.
- Mock Data Creation:
- Inside the method, I create instances of
AnforderungssichtDataElement, AusgabeData, and VersandDataAndServiceProvider.
- For
ausgabeData1 and provider1 (and ausgabeData2, provider2 for the second element), I call the respective set...() methods to put mock values into their fields.
- Complex fields like
dokumentData and detailData are set to null as their structure wasn't provided and mocking them would require defining those classes as well.
- The populated
ausgabeData and provider objects are then set into the AnforderungssichtDataElement using its setData() and setProvider() methods.
- Returning the List:
- An
ArrayList is created.
- The populated
AnforderungssichtDataElement objects (element1, element2) are added to the list.
- The method returns this list. I've shown how to return a list with multiple elements (more typical for a list endpoint) but also commented on how to return just one using
Collections.singletonList() if you strictly wanted to match the template's return structure.
To run this, you would need a Spring Boot application setup with the necessary dependencies (Spring Web, and potentially springdoc-openapi for the @Operation annotation).