Skip to content

Commit

Permalink
Konfig-Anpassungen für SpringDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Oli B committed Feb 8, 2023
1 parent cd36860 commit 273de72
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 20 deletions.
27 changes: 17 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
<properties>

<patterntesting.version>2.1.2</patterntesting.version>
<spring-boot.version>2.5.14</spring-boot.version>
<spring-boot.version>2.6.14</spring-boot.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>

<main.class>gdv.xport.Main</main.class>

Expand Down Expand Up @@ -553,20 +553,27 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.14.1</version>
<optional>true</optional>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.14.1</version>
<optional>true</optional>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.1</version>
<optional>true</optional>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-base</artifactId>
<version>2.13.4</version>
</dependency>

<!-- JavaBeans Activation Framework (JAF) -->
Expand Down
12 changes: 12 additions & 0 deletions service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
Expand All @@ -62,6 +66,10 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-base</artifactId>
</dependency>

<!-- Spring Testing -->
<dependency>
Expand All @@ -75,6 +83,10 @@
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-webmvc-core</artifactId>
</dependency>

<!-- Thymeleaf + WebJars (see https://igorski.co/java/spring-boot/layout-dialect-spring-boot-2/) -->
<dependency>
Expand Down
28 changes: 20 additions & 8 deletions service/src/main/java/gdv/xport/srv/config/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@
*/
package gdv.xport.srv.config;

import gdv.xport.srv.web.converter.*;
import org.apache.logging.log4j.*;
import org.springframework.context.annotation.*;
import org.springframework.http.*;
import org.springframework.http.converter.*;
import org.springframework.web.servlet.config.annotation.*;
import gdv.xport.srv.web.converter.DatenpaketHttpMessageConverter;
import gdv.xport.srv.web.converter.ErrorDetailHttpMessageConverter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.*;
import java.util.List;

/**
* Ueber AppConfig werden einige Konfigurationseinstellungen vorgenommen.
Expand Down Expand Up @@ -61,14 +68,19 @@ public void addInterceptors(InterceptorRegistry registry) {
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// see https://github.com/springdoc/springdoc-openapi/issues/624
converters.add(new StringHttpMessageConverter());
converters.add(new MappingJackson2HttpMessageConverter());
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new DatenpaketHttpMessageConverter(MediaType.TEXT_HTML));
converters.add(new DatenpaketHttpMessageConverter(MediaType.TEXT_XML, MediaType.APPLICATION_XML));
converters.add(new DatenpaketHttpMessageConverter(MediaType.TEXT_PLAIN));
converters.add(new DatenpaketHttpMessageConverter(MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8));
converters.add(new DatenpaketHttpMessageConverter(MediaType.APPLICATION_JSON));
converters.add(new DatenpaketHttpMessageConverter(MEDIA_TYPE_TEXT_CSV));
converters.add(new ErrorDetailHttpMessageConverter(MediaType.TEXT_HTML));
converters.add(new ErrorDetailHttpMessageConverter(MediaType.TEXT_XML, MediaType.APPLICATION_XML));
converters.add(new ErrorDetailHttpMessageConverter(MediaType.TEXT_PLAIN));
//converters.add(new ErrorDetailHttpMessageConverter(MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8));
converters.add(new ErrorDetailHttpMessageConverter(MEDIA_TYPE_TEXT_CSV));
LOG.info("Message converters {} are configured.", converters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package gdv.xport.srv.web.converter;

import com.fasterxml.jackson.databind.ObjectMapper;
import gdv.xport.srv.web.*;
import org.apache.logging.log4j.*;
import org.springframework.http.*;
Expand All @@ -36,6 +37,7 @@
public final class ErrorDetailHttpMessageConverter extends AbstractHttpMessageConverter<ErrorDetail> {

private static final Logger LOG = LogManager.getLogger(ErrorDetailHttpMessageConverter.class);
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

/**
* Erzeugt eine MessageConverter fuer die angegebene Media-Typen.
Expand Down Expand Up @@ -78,6 +80,9 @@ protected void writeInternal(ErrorDetail errorDetail, HttpOutputMessage outputMe
case TEXT_CSV:
writeInternalCSV(errorDetail, writer);
break;
case MediaType.APPLICATION_JSON_VALUE:
OBJECT_MAPPER.writeValue(writer, errorDetail);
break;
default:
writer.write(errorDetail.toString());
writer.write('\n');
Expand Down
4 changes: 4 additions & 0 deletions service/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ spring.profiles.active=default

# Port 2517 ist 0x9D5 =~ gDV
server.port=2517

springdoc.api-docs.path=/v3/api-docs
springdoc.swagger-ui.config-url=/v3/api-docs/swagger-config
springdoc.writer-with-default-pretty-printer=true
9 changes: 9 additions & 0 deletions service/src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ <h1>GDV.XPort-Services <span th:text="${appVersion}"></span></h1>
<a href="/v2/api-docs">/v2/api-docs</a> und online als
<a href="swagger-ui/">Swagger-Doku</a> abrufbar.
</p>
<dl>
<dt>Hinweis:</dt>
<dd>
Wenn beim Aufuf der <a href="swagger-ui/">Swagger-Doku</a>
ein <i>"Failed to load remote configuration"</i> kommt,
einfach <code>"/v3/api-docs"</code> im Eingabefeld eingeben und
<b>Explore</b> dr&uuml;cken.
</dd>
</dl>
</div>
</div>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,41 @@
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
* Integrationstests fuer den {@link DatenpaketController}.
*
* @author <a href="[email protected]">oliver</a>
*/
@RunWith(SpringRunner.class)
public final class DatenpaketControllerIT extends AbstractControllerIT {
public final class

























DatenpaketControllerIT extends AbstractControllerIT {

private static final Logger LOG = LogManager.getLogger(DatenpaketControllerIT.class);

Expand Down Expand Up @@ -163,7 +190,8 @@ public void testErrorDetailAsHTML() {
private void checkMalformedURL(String format) {
ResponseEntity<String> response =
getResponseEntityFor("/api/v1/Datenpaket." + format + "?uri=xxx:gibts.net", String.class);
MatcherAssert.assertThat(response.getBody().toLowerCase(), containsString("status"));
assertNotNull(response.getBody());
MatcherAssert.assertThat(response.getBody().toLowerCase(), containsString("bad"));
MatcherAssert.assertThat(response.getBody(), not(containsString("500")));
}

Expand Down

0 comments on commit 273de72

Please sign in to comment.