Hydra EntryPoint erstellt. vocab.jsonld angepasst
This commit is contained in:
@@ -7,8 +7,8 @@ import com.kapdion.omds.productdefinitions.calculate.CalculateService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@@ -16,13 +16,14 @@ import java.io.IOException;
|
||||
public class EndpointsZentralesBOA {
|
||||
|
||||
@PostMapping("/ProductsRequest")
|
||||
public String apriori(@RequestBody ProductsRequest productsRequest){
|
||||
public HydraResponse<List<Object>> apriori(@RequestBody ProductsRequest productsRequest){
|
||||
AprioriService as = new AprioriService();
|
||||
String s = as.getProductsResponse(productsRequest);
|
||||
List<Object> s = Collections.singletonList(as.getProductsResponse(productsRequest));
|
||||
HydraResponse<List<Object>> r = new HydraResponse<>(s);
|
||||
System.out.println("-----------------------");
|
||||
System.out.println("Products request: " + s);
|
||||
System.out.println("Products request: " + r.data.toString());
|
||||
|
||||
return s;
|
||||
return r;
|
||||
};
|
||||
|
||||
@PostMapping("/CalculateRequest")
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.kapdion.omds.productdefinitions;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@CrossOrigin(origins = "*", exposedHeaders = "Link")
|
||||
public class EntryPointController {
|
||||
|
||||
@GetMapping(value = "/", produces = "application/ld+json")
|
||||
public ResponseEntity<String> getEntryPoint() {
|
||||
|
||||
String body = """
|
||||
{
|
||||
"@context": "http://localhost:9090/produktwissen-app/produktApi/context.jsonld",
|
||||
"@id": "http://localhost:9090/produktwissen-app/produktApi/",
|
||||
"@type": "hydra:EntryPoint"
|
||||
}
|
||||
""";
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.header(
|
||||
"Link",
|
||||
"<http://localhost:9090/produktwissen-app/produktApi/vocab.jsonld>; rel=\"http://www.w3.org/ns/hydra/core#apiDocumentation\""
|
||||
)
|
||||
.body(body);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.kapdion.omds.productdefinitions;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/produktApi") // Dein Pfad
|
||||
public class HydraController {
|
||||
|
||||
@GetMapping(value = "/vocab", produces = "application/ld+json")
|
||||
public Resource getVocab() {
|
||||
// Lädt die Datei aus dem api-definition Modul
|
||||
return new ClassPathResource("vocab.jsonld");
|
||||
}
|
||||
|
||||
@GetMapping(value = "/context", produces = "application/ld+json")
|
||||
public Resource getContext() {
|
||||
return new ClassPathResource("context.jsonld");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.kapdion.omds.productdefinitions;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@CrossOrigin(origins = "*") // UNBEDINGT HIER AUCH HINZUFÜGEN
|
||||
public class HydraDocController {
|
||||
|
||||
// Lädt die Datei aus dem Classpath (kommt aus dem api-definition Modul)
|
||||
private Resource loadFile(String name) {
|
||||
return new ClassPathResource(name);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/context.jsonld", produces = "application/ld+json")
|
||||
public Resource getContext() {
|
||||
return loadFile("context.jsonld");
|
||||
}
|
||||
|
||||
@GetMapping(value = "/vocab.jsonld", produces = "application/ld+json")
|
||||
public Resource getVocab() {
|
||||
return loadFile("vocab.jsonld");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.kapdion.omds.productdefinitions;
|
||||
|
||||
import jakarta.servlet.*;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
public class HydraHeaderFilter implements Filter {
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
|
||||
|
||||
// Der Link zeigt auf dein Vocab-File
|
||||
String linkHeader = "<http://localhost:9090/produktwissen-app/produktApi/vocab.jsonld>; rel=\"http://www.w3.org/ns/hydra/core#apiDocumentation\"";
|
||||
httpServletResponse.setHeader("Link", linkHeader);
|
||||
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.kapdion.omds.productdefinitions;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class HydraResponse<T> {
|
||||
@JsonProperty("@context")
|
||||
public String context = "/produktwissen-app/produktApi/context";
|
||||
|
||||
@JsonProperty("@type")
|
||||
public String type = "api:CalculateResponse";
|
||||
|
||||
@JsonProperty("@graph")
|
||||
public T data;
|
||||
|
||||
public HydraResponse(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.kapdion.omds.productdefinitions;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
||||
.allowedOrigins("*")
|
||||
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
||||
.exposedHeaders("Link"); // WICHTIG: Damit die Konsole den Link-Header lesen kann!
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
spring.application.name=productdefinitions
|
||||
server.port=9090
|
||||
|
||||
server.servlet.context-path=/produktApi
|
||||
server.servlet.context-path=/produktwissen-app/produktApi
|
||||
logging.level.org.springframework.web=DEBUG
|
||||
server.tomcat.relaxed-header-chars=:,
|
||||
|
||||
Reference in New Issue
Block a user