Polishing
This commit is contained in:
@@ -24,11 +24,12 @@ import java.sql.Timestamp;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.core.convert.support.DefaultConversionService;
|
import org.springframework.core.convert.support.DefaultConversionService;
|
||||||
import org.springframework.dao.TypeMismatchDataAccessException;
|
import org.springframework.dao.TypeMismatchDataAccessException;
|
||||||
|
|
||||||
import static org.mockito.BDDMockito.*;
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import static org.mockito.BDDMockito.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link SingleColumnRowMapper}.
|
* Tests for {@link SingleColumnRowMapper}.
|
||||||
@@ -38,7 +39,7 @@ import static org.junit.Assert.*;
|
|||||||
*/
|
*/
|
||||||
public class SingleColumnRowMapperTests {
|
public class SingleColumnRowMapperTests {
|
||||||
|
|
||||||
@Test // SPR-16483
|
@Test // SPR-16483
|
||||||
public void useDefaultConversionService() throws SQLException {
|
public void useDefaultConversionService() throws SQLException {
|
||||||
Timestamp timestamp = new Timestamp(0);
|
Timestamp timestamp = new Timestamp(0);
|
||||||
|
|
||||||
@@ -57,7 +58,7 @@ public class SingleColumnRowMapperTests {
|
|||||||
assertEquals(timestamp.toLocalDateTime(), actualLocalDateTime);
|
assertEquals(timestamp.toLocalDateTime(), actualLocalDateTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // SPR-16483
|
@Test // SPR-16483
|
||||||
public void useCustomConversionService() throws SQLException {
|
public void useCustomConversionService() throws SQLException {
|
||||||
Timestamp timestamp = new Timestamp(0);
|
Timestamp timestamp = new Timestamp(0);
|
||||||
|
|
||||||
@@ -81,7 +82,7 @@ public class SingleColumnRowMapperTests {
|
|||||||
assertEquals(timestamp.toLocalDateTime(), actualMyLocalDateTime.value);
|
assertEquals(timestamp.toLocalDateTime(), actualMyLocalDateTime.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = TypeMismatchDataAccessException.class) // SPR-16483
|
@Test(expected = TypeMismatchDataAccessException.class) // SPR-16483
|
||||||
public void doesNotUseConversionService() throws SQLException {
|
public void doesNotUseConversionService() throws SQLException {
|
||||||
SingleColumnRowMapper<LocalDateTime> rowMapper =
|
SingleColumnRowMapper<LocalDateTime> rowMapper =
|
||||||
SingleColumnRowMapper.newInstance(LocalDateTime.class, null);
|
SingleColumnRowMapper.newInstance(LocalDateTime.class, null);
|
||||||
@@ -97,9 +98,12 @@ public class SingleColumnRowMapperTests {
|
|||||||
rowMapper.mapRow(resultSet, 1);
|
rowMapper.mapRow(resultSet, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class MyLocalDateTime {
|
private static class MyLocalDateTime {
|
||||||
|
|
||||||
private final LocalDateTime value;
|
private final LocalDateTime value;
|
||||||
private MyLocalDateTime(LocalDateTime value) {
|
|
||||||
|
public MyLocalDateTime(LocalDateTime value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2017 the original author or authors.
|
* Copyright 2002-2018 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -126,10 +126,8 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
|
|||||||
String responseETag = generateETagHeaderValue(responseWrapper.getContentInputStream(), this.writeWeakETag);
|
String responseETag = generateETagHeaderValue(responseWrapper.getContentInputStream(), this.writeWeakETag);
|
||||||
rawResponse.setHeader(HEADER_ETAG, responseETag);
|
rawResponse.setHeader(HEADER_ETAG, responseETag);
|
||||||
String requestETag = request.getHeader(HEADER_IF_NONE_MATCH);
|
String requestETag = request.getHeader(HEADER_IF_NONE_MATCH);
|
||||||
if (requestETag != null
|
if (requestETag != null && ("*".equals(requestETag) || responseETag.equals(requestETag) ||
|
||||||
&& (responseETag.equals(requestETag)
|
responseETag.replaceFirst("^W/", "").equals(requestETag.replaceFirst("^W/", "")))) {
|
||||||
|| responseETag.replaceFirst("^W/", "").equals(requestETag.replaceFirst("^W/", ""))
|
|
||||||
|| "*".equals(requestETag))) {
|
|
||||||
if (logger.isTraceEnabled()) {
|
if (logger.isTraceEnabled()) {
|
||||||
logger.trace("ETag [" + responseETag + "] equal to If-None-Match, sending 304");
|
logger.trace("ETag [" + responseETag + "] equal to If-None-Match, sending 304");
|
||||||
}
|
}
|
||||||
@@ -163,19 +161,15 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
|
|||||||
* @param response the HTTP response
|
* @param response the HTTP response
|
||||||
* @param responseStatusCode the HTTP response status code
|
* @param responseStatusCode the HTTP response status code
|
||||||
* @param inputStream the response body
|
* @param inputStream the response body
|
||||||
* @return {@code true} if eligible for ETag generation; {@code false} otherwise
|
* @return {@code true} if eligible for ETag generation, {@code false} otherwise
|
||||||
*/
|
*/
|
||||||
protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response,
|
protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response,
|
||||||
int responseStatusCode, InputStream inputStream) {
|
int responseStatusCode, InputStream inputStream) {
|
||||||
|
|
||||||
String method = request.getMethod();
|
String method = request.getMethod();
|
||||||
if (responseStatusCode >= 200 && responseStatusCode < 300
|
if (responseStatusCode >= 200 && responseStatusCode < 300 && HttpMethod.GET.matches(method)) {
|
||||||
&& HttpMethod.GET.matches(method)) {
|
|
||||||
|
|
||||||
String cacheControl = response.getHeader(HEADER_CACHE_CONTROL);
|
String cacheControl = response.getHeader(HEADER_CACHE_CONTROL);
|
||||||
if (cacheControl == null || !cacheControl.contains(DIRECTIVE_NO_STORE)) {
|
return (cacheControl == null || !cacheControl.contains(DIRECTIVE_NO_STORE));
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -189,7 +183,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
|
|||||||
* @see org.springframework.util.DigestUtils
|
* @see org.springframework.util.DigestUtils
|
||||||
*/
|
*/
|
||||||
protected String generateETagHeaderValue(InputStream inputStream, boolean isWeak) throws IOException {
|
protected String generateETagHeaderValue(InputStream inputStream, boolean isWeak) throws IOException {
|
||||||
// length of W/ + 0 + " + 32bits md5 hash + "
|
// length of W/ + " + 0 + 32bits md5 hash + "
|
||||||
StringBuilder builder = new StringBuilder(37);
|
StringBuilder builder = new StringBuilder(37);
|
||||||
if (isWeak) {
|
if (isWeak) {
|
||||||
builder.append("W/");
|
builder.append("W/");
|
||||||
|
|||||||
@@ -1106,7 +1106,7 @@ The `number` package provides a `NumberFormatter`, `CurrencyFormatter`, and
|
|||||||
`PercentFormatter` to format `java.lang.Number` objects using a `java.text.NumberFormat`.
|
`PercentFormatter` to format `java.lang.Number` objects using a `java.text.NumberFormat`.
|
||||||
The `datetime` package provides a `DateFormatter` to format `java.util.Date` objects with
|
The `datetime` package provides a `DateFormatter` to format `java.util.Date` objects with
|
||||||
a `java.text.DateFormat`. The `datetime.joda` package provides comprehensive datetime
|
a `java.text.DateFormat`. The `datetime.joda` package provides comprehensive datetime
|
||||||
formatting support based on the http://joda-time.sourceforge.net[Joda Time library].
|
formatting support based on the http://joda-time.sourceforge.net[Joda-Time library].
|
||||||
|
|
||||||
Consider `DateFormatter` as an example `Formatter` implementation:
|
Consider `DateFormatter` as an example `Formatter` implementation:
|
||||||
|
|
||||||
@@ -1240,7 +1240,7 @@ To trigger formatting, simply annotate fields with @NumberFormat:
|
|||||||
|
|
||||||
A portable format annotation API exists in the `org.springframework.format.annotation`
|
A portable format annotation API exists in the `org.springframework.format.annotation`
|
||||||
package. Use @NumberFormat to format java.lang.Number fields. Use @DateTimeFormat to
|
package. Use @NumberFormat to format java.lang.Number fields. Use @DateTimeFormat to
|
||||||
format java.util.Date, java.util.Calendar, java.util.Long, or Joda Time fields.
|
format java.util.Date, java.util.Calendar, java.util.Long, or Joda-Time fields.
|
||||||
|
|
||||||
The example below uses @DateTimeFormat to format a java.util.Date as a ISO Date
|
The example below uses @DateTimeFormat to format a java.util.Date as a ISO Date
|
||||||
(yyyy-MM-dd):
|
(yyyy-MM-dd):
|
||||||
@@ -1344,10 +1344,10 @@ You will need to ensure that Spring does not register default formatters, and in
|
|||||||
you should register all formatters manually. Use the
|
you should register all formatters manually. Use the
|
||||||
`org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar` or
|
`org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar` or
|
||||||
`org.springframework.format.datetime.DateFormatterRegistrar` class depending on whether
|
`org.springframework.format.datetime.DateFormatterRegistrar` class depending on whether
|
||||||
you use the Joda Time library.
|
you use the Joda-Time library.
|
||||||
|
|
||||||
For example, the following Java configuration will register a global ' `yyyyMMdd`'
|
For example, the following Java configuration will register a global ' `yyyyMMdd`'
|
||||||
format. This example does not depend on the Joda Time library:
|
format. This example does not depend on the Joda-Time library:
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
@@ -1412,7 +1412,7 @@ Time:
|
|||||||
|
|
||||||
[NOTE]
|
[NOTE]
|
||||||
====
|
====
|
||||||
Joda Time provides separate distinct types to represent `date`, `time` and `date-time`
|
Joda-Time provides separate distinct types to represent `date`, `time` and `date-time`
|
||||||
values. The `dateFormatter`, `timeFormatter` and `dateTimeFormatter` properties of the
|
values. The `dateFormatter`, `timeFormatter` and `dateTimeFormatter` properties of the
|
||||||
`JodaTimeFormatterRegistrar` should be used to configure the different formats for each
|
`JodaTimeFormatterRegistrar` should be used to configure the different formats for each
|
||||||
type. The `DateTimeFormatterFactoryBean` provides a convenient way to create formatters.
|
type. The `DateTimeFormatterFactoryBean` provides a convenient way to create formatters.
|
||||||
|
|||||||
@@ -2452,8 +2452,8 @@ In your Java config implement the `WebFluxConfigurer` interface:
|
|||||||
[.small]#<<web.adoc#mvc-config-conversion,Same in Spring MVC>>#
|
[.small]#<<web.adoc#mvc-config-conversion,Same in Spring MVC>>#
|
||||||
|
|
||||||
By default formatters for `Number` and `Date` types are installed, including support for
|
By default formatters for `Number` and `Date` types are installed, including support for
|
||||||
the `@NumberFormat` and `@DateTimeFormat` annotations. Full support for the Joda Time
|
the `@NumberFormat` and `@DateTimeFormat` annotations. Full support for the Joda-Time
|
||||||
formatting library is also installed if Joda Time is present on the classpath.
|
formatting library is also installed if Joda-Time is present on the classpath.
|
||||||
|
|
||||||
To register custom formatters and converters:
|
To register custom formatters and converters:
|
||||||
|
|
||||||
|
|||||||
@@ -277,10 +277,10 @@ The table below lists the special beans detected by the `DispatcherHandler`:
|
|||||||
=== Web MVC Config
|
=== Web MVC Config
|
||||||
[.small]#<<web-reactive.adoc#webflux-framework-config,Same in Spring WebFlux>>#
|
[.small]#<<web-reactive.adoc#webflux-framework-config,Same in Spring WebFlux>>#
|
||||||
|
|
||||||
Applications can declare the infrastructure beans listed in <<mvc-special-bean-types>>
|
Applications can declare the infrastructure beans listed in <<mvc-servlet-special-bean-types>>
|
||||||
that are required to process requests. The `DispatcherServlet` checks the
|
that are required to process requests. The `DispatcherServlet` checks the
|
||||||
`WebApplicationContext` for each special bean. If there are no matching bean types, it
|
`WebApplicationContext` for each special bean. If there are no matching bean types,
|
||||||
falls back on the default types listed in
|
it falls back on the default types listed in
|
||||||
https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/resources/org/springframework/web/servlet/DispatcherServlet.properties[DispatcherServlet.properties].
|
https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/resources/org/springframework/web/servlet/DispatcherServlet.properties[DispatcherServlet.properties].
|
||||||
|
|
||||||
In most cases the <<mvc-config>> is the best starting point. It declares the required
|
In most cases the <<mvc-config>> is the best starting point. It declares the required
|
||||||
@@ -3733,9 +3733,9 @@ applications along with a configuration API to customize it.
|
|||||||
For more advanced customizations, not available in the configuration API, see
|
For more advanced customizations, not available in the configuration API, see
|
||||||
<<mvc-config-advanced-java>> and <<mvc-config-advanced-xml>>.
|
<<mvc-config-advanced-java>> and <<mvc-config-advanced-xml>>.
|
||||||
|
|
||||||
You do not need to understand the underlying beans created by the MVC Java config and the
|
You do not need to understand the underlying beans created by the MVC Java config and
|
||||||
MVC namespace but if you want to learn more, see <<mvc-servlet-special-bean-types>> and
|
the MVC namespace but if you want to learn more, see <<mvc-servlet-special-bean-types>>
|
||||||
<<mvc-servlet-config>>.
|
and <<mvc-servlet-config>>.
|
||||||
|
|
||||||
|
|
||||||
[[mvc-config-enable]]
|
[[mvc-config-enable]]
|
||||||
@@ -3796,10 +3796,10 @@ In Java config implement `WebMvcConfigurer` interface:
|
|||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
In XML check attributes and sub-elements of `<mvc:annotation-driven/>`. You can view the
|
In XML check attributes and sub-elements of `<mvc:annotation-driven/>`. You can
|
||||||
http://schema.spring.io/mvc/spring-mvc.xsd[Spring MVC XML schema] or use the code
|
view the http://schema.spring.io/mvc/spring-mvc.xsd[Spring MVC XML schema] or use
|
||||||
completion feature of your IDE to discover what attributes and sub-elements are
|
the code completion feature of your IDE to discover what attributes and
|
||||||
available.
|
sub-elements are available.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -3808,8 +3808,8 @@ available.
|
|||||||
[.small]#<<web-reactive.adoc#webflux-config-conversion,Same in Spring WebFlux>>#
|
[.small]#<<web-reactive.adoc#webflux-config-conversion,Same in Spring WebFlux>>#
|
||||||
|
|
||||||
By default formatters for `Number` and `Date` types are installed, including support for
|
By default formatters for `Number` and `Date` types are installed, including support for
|
||||||
the `@NumberFormat` and `@DateTimeFormat` annotations. Full support for the Joda Time
|
the `@NumberFormat` and `@DateTimeFormat` annotations. Full support for the Joda-Time
|
||||||
formatting library is also installed if Joda Time is present on the classpath.
|
formatting library is also installed if Joda-Time is present on the classpath.
|
||||||
|
|
||||||
In Java config, register custom formatters and converters:
|
In Java config, register custom formatters and converters:
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user