Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
1cbef022
Commit
1cbef022
authored
Apr 28, 2014
by
Marcel Overdijk
Committed by
Dave Syer
Apr 30, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add messagecode resolver format based on application property
parent
e26e06d5
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
7 deletions
+46
-7
WebMvcAutoConfiguration.java
...ework/boot/autoconfigure/web/WebMvcAutoConfiguration.java
+14
-1
WebMvcAutoConfigurationTests.java
.../boot/autoconfigure/web/WebMvcAutoConfigurationTests.java
+31
-6
appendix-application-properties.adoc
...cs/src/main/asciidoc/appendix-application-properties.adoc
+1
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java
View file @
1cbef022
...
...
@@ -31,7 +31,6 @@ import org.springframework.beans.factory.ListableBeanFactory;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.AutoConfigureAfter
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
;
...
...
@@ -50,6 +49,8 @@ import org.springframework.format.Formatter;
import
org.springframework.format.FormatterRegistry
;
import
org.springframework.http.converter.HttpMessageConverter
;
import
org.springframework.util.StringUtils
;
import
org.springframework.validation.DefaultMessageCodesResolver
;
import
org.springframework.validation.MessageCodesResolver
;
import
org.springframework.web.accept.ContentNegotiationManager
;
import
org.springframework.web.context.request.RequestContextListener
;
import
org.springframework.web.filter.HiddenHttpMethodFilter
;
...
...
@@ -135,6 +136,9 @@ public class WebMvcAutoConfiguration {
@Value
(
"${spring.resources.cachePeriod:}"
)
private
Integer
cachePeriod
;
@Value
(
"${spring.mvc.message-codes-resolver.format:}"
)
private
String
messageCodesResolverFormat
=
""
;
@Value
(
"${spring.mvc.locale:}"
)
private
String
locale
=
""
;
...
...
@@ -195,6 +199,15 @@ public class WebMvcAutoConfiguration {
return
new
FixedLocaleResolver
(
StringUtils
.
parseLocaleString
(
this
.
locale
));
}
@Bean
@ConditionalOnMissingBean
(
MessageCodesResolver
.
class
)
@ConditionalOnExpression
(
"'${spring.mvc.message-codes-resolver.format:}' != ''"
)
public
MessageCodesResolver
messageCodesResolver
()
{
DefaultMessageCodesResolver
resolver
=
new
DefaultMessageCodesResolver
();
resolver
.
setMessageCodeFormatter
(
DefaultMessageCodesResolver
.
Format
.
valueOf
(
messageCodesResolverFormat
));
return
resolver
;
}
@Override
public
void
addFormatters
(
FormatterRegistry
registry
)
{
for
(
Converter
<?,
?>
converter
:
getBeansOfType
(
Converter
.
class
))
{
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java
View file @
1cbef022
...
...
@@ -16,6 +16,12 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
web
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
hamcrest
.
Matchers
.
instanceOf
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
java.lang.reflect.Field
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
...
...
@@ -43,6 +49,7 @@ import org.springframework.core.io.Resource;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.util.ReflectionUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.validation.MessageCodesResolver
;
import
org.springframework.web.servlet.HandlerAdapter
;
import
org.springframework.web.servlet.HandlerMapping
;
import
org.springframework.web.servlet.LocaleResolver
;
...
...
@@ -55,12 +62,6 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
import
org.springframework.web.servlet.resource.ResourceHttpRequestHandler
;
import
org.springframework.web.servlet.view.AbstractView
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
hamcrest
.
Matchers
.
instanceOf
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertThat
;
/**
* Tests for {@link WebMvcAutoConfiguration}.
*
...
...
@@ -180,6 +181,30 @@ public class WebMvcAutoConfigurationTests {
assertThat
(
locale
.
toString
(),
equalTo
(
"en_UK"
));
}
@Test
(
expected
=
NoSuchBeanDefinitionException
.
class
)
public
void
noMessageCodeResolver
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigEmbeddedWebApplicationContext
();
this
.
context
.
register
(
AllResources
.
class
,
Config
.
class
,
WebMvcAutoConfiguration
.
class
,
HttpMessageConvertersAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
this
.
context
.
getBean
(
MessageCodesResolver
.
class
);
}
@Test
public
void
overrideMessageCodesFormat
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigEmbeddedWebApplicationContext
();
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.mvc.message-codes-resolver.format:POSTFIX_ERROR_CODE"
);
this
.
context
.
register
(
AllResources
.
class
,
Config
.
class
,
WebMvcAutoConfiguration
.
class
,
HttpMessageConvertersAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
this
.
context
.
getBean
(
MessageCodesResolver
.
class
);
}
@SuppressWarnings
(
"unchecked"
)
protected
Map
<
String
,
List
<
Resource
>>
getMappingLocations
()
throws
IllegalAccessException
{
...
...
spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc
View file @
1cbef022
...
...
@@ -68,6 +68,7 @@ content into your application; rather pick only the properties that you need.
http.mappers.json-pretty-print=false # pretty print JSON
http.mappers.json-sort-keys=false # sort keys
spring.mvc.locale= # set fixed locale, e.g. en_UK
spring.mvc.message-codes-resolver.format= # PREFIX_ERROR_CODE / POSTFIX_ERROR_CODE
spring.view.prefix= # MVC view prefix
spring.view.suffix= # ... and suffix
spring.resources.cache-period= # cache timeouts in headers sent to browser
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment