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
23ebf017
Commit
23ebf017
authored
Feb 04, 2021
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract code samples from docs
See gh-6313
parent
7a3bd6d4
Changes
26
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
26 changed files
with
1279 additions
and
317 deletions
+1279
-317
build.gradle
spring-boot-project/spring-boot-docs/build.gradle
+1
-0
spring-boot-features.adoc
...ing-boot-docs/src/docs/asciidoc/spring-boot-features.adoc
+55
-317
ThirdPartyConfiguration.java
...es/externalizedconfiguration/ThirdPartyConfiguration.java
+38
-0
MyApplication.java
...tures/externalizedconfiguration/enable/MyApplication.java
+28
-0
MyConfiguration.java
...res/externalizedconfiguration/enable/MyConfiguration.java
+32
-0
AcmeProperties.java
.../externalizedconfiguration/merge/list/AcmeProperties.java
+39
-0
AcmeProperties.java
...s/externalizedconfiguration/merge/map/AcmeProperties.java
+39
-0
OwnerProperties.java
...es/externalizedconfiguration/relaxed/OwnerProperties.java
+36
-0
MyService.java
...bootfeatures/externalizedconfiguration/use/MyService.java
+58
-0
AcmeProperties.java
...es/externalizedconfiguration/validate/AcmeProperties.java
+43
-0
AcmeProperties.java
...rnalizedconfiguration/validate/nested/AcmeProperties.java
+67
-0
ProductionConfiguration.java
.../springbootfeatures/profiles/ProductionConfiguration.java
+30
-0
HttpMessageConvertersConfiguration.java
...s/webapplications/HttpMessageConvertersConfiguration.java
+67
-0
MyJsonComponent.java
...ingbootfeatures/webapplications/json/MyJsonComponent.java
+78
-0
MyJsonComponent.java
...features/webapplications/json/object/MyJsonComponent.java
+76
-0
CorsConfiguration.java
...otfeatures/webapplications/servlet/CorsConfiguration.java
+41
-0
ErrorPageConfiguration.java
...tures/webapplications/servlet/ErrorPageConfiguration.java
+40
-0
MyControllerAdvice.java
...tfeatures/webapplications/servlet/MyControllerAdvice.java
+62
-0
MyErrorViewResolver.java
...features/webapplications/servlet/MyErrorViewResolver.java
+41
-0
MyRestController.java
...ootfeatures/webapplications/servlet/MyRestController.java
+80
-0
ServletFilterConfiguration.java
...s/webapplications/servlet/ServletFilterConfiguration.java
+55
-0
CodecConfiguration.java
...tfeatures/webapplications/webflux/CodecConfiguration.java
+38
-0
CustomErrorWebExceptionHandler.java
...bapplications/webflux/CustomErrorWebExceptionHandler.java
+59
-0
MyRestController.java
...ootfeatures/webapplications/webflux/MyRestController.java
+83
-0
RoutingConfiguration.java
...ures/webapplications/webflux/fn/RoutingConfiguration.java
+48
-0
UserHandler.java
...gbootfeatures/webapplications/webflux/fn/UserHandler.java
+45
-0
No files found.
spring-boot-project/spring-boot-docs/build.gradle
View file @
23ebf017
...
...
@@ -86,6 +86,7 @@ dependencies {
implementation
(
"org.slf4j:jul-to-slf4j"
)
implementation
(
"org.springframework:spring-test"
)
implementation
(
"org.springframework:spring-web"
)
implementation
(
"org.springframework:spring-webmvc"
)
implementation
(
"org.springframework:spring-webflux"
)
implementation
(
"org.springframework.data:spring-data-couchbase"
)
implementation
(
"org.springframework.data:spring-data-neo4j"
)
...
...
spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc
View file @
23ebf017
This diff is collapsed.
Click to expand it.
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/ThirdPartyConfiguration.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
externalizedconfiguration
;
//tag::code[]
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
@Configuration
(
proxyBeanMethods
=
false
)
public
class
ThirdPartyConfiguration
{
@Bean
@ConfigurationProperties
(
prefix
=
"another"
)
public
AnotherComponent
anotherComponent
()
{
return
new
AnotherComponent
();
}
}
// end::code[]
class
AnotherComponent
{
}
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/enable/MyApplication.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
externalizedconfiguration
.
enable
;
//tag::code[]
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.boot.context.properties.ConfigurationPropertiesScan
;
@SpringBootApplication
@ConfigurationPropertiesScan
({
"com.example.app"
,
"org.acme.another"
})
public
class
MyApplication
{
}
// end::code[]
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/enable/MyConfiguration.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
externalizedconfiguration
.
enable
;
//tag::code[]
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.annotation.Configuration
;
@Configuration
(
proxyBeanMethods
=
false
)
@EnableConfigurationProperties
(
AcmeProperties
.
class
)
public
class
MyConfiguration
{
}
// end::code[]
class
AcmeProperties
{
}
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/merge/list/AcmeProperties.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
externalizedconfiguration
.
merge
.
list
;
//tag::code[]
import
java.util.ArrayList
;
import
java.util.List
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
@ConfigurationProperties
(
"acme"
)
public
class
AcmeProperties
{
private
final
List
<
MyPojo
>
list
=
new
ArrayList
<>();
public
List
<
MyPojo
>
getList
()
{
return
this
.
list
;
}
}
// end::code[]
class
MyPojo
{
}
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/merge/map/AcmeProperties.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
externalizedconfiguration
.
merge
.
map
;
//tag::code[]
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
@ConfigurationProperties
(
"acme"
)
public
class
AcmeProperties
{
private
final
Map
<
String
,
MyPojo
>
map
=
new
LinkedHashMap
<>();
public
Map
<
String
,
MyPojo
>
getMap
()
{
return
this
.
map
;
}
}
// end::code[]
class
MyPojo
{
}
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/relaxed/OwnerProperties.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
externalizedconfiguration
.
relaxed
;
//tag::code[]
import
org.springframework.boot.context.properties.ConfigurationProperties
;
@ConfigurationProperties
(
prefix
=
"acme.my-project.person"
)
public
class
OwnerProperties
{
private
String
firstName
;
public
String
getFirstName
()
{
return
this
.
firstName
;
}
public
void
setFirstName
(
String
firstName
)
{
this
.
firstName
=
firstName
;
}
}
// end::code[]
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/use/MyService.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
externalizedconfiguration
.
use
;
//tag::code[]
import
org.springframework.stereotype.Service
;
@Service
public
class
MyService
{
private
final
AcmeProperties
properties
;
public
MyService
(
AcmeProperties
properties
)
{
this
.
properties
=
properties
;
}
public
void
openConnection
()
{
Server
server
=
new
Server
(
this
.
properties
.
getRemoteAddress
());
server
.
start
();
// ...
}
// ...
}
// end::code[]
class
AcmeProperties
{
Object
getRemoteAddress
()
{
return
null
;
}
}
class
Server
{
Server
(
Object
remoteAddress
)
{
}
void
start
()
{
}
}
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/validate/AcmeProperties.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
externalizedconfiguration
.
validate
;
//tag::code[]
import
java.net.InetAddress
;
import
javax.validation.constraints.NotNull
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.validation.annotation.Validated
;
@ConfigurationProperties
(
prefix
=
"acme"
)
@Validated
public
class
AcmeProperties
{
@NotNull
private
InetAddress
remoteAddress
;
public
InetAddress
getRemoteAddress
()
{
return
this
.
remoteAddress
;
}
public
void
setRemoteAddress
(
InetAddress
remoteAddress
)
{
this
.
remoteAddress
=
remoteAddress
;
}
}
// end::code[]
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/validate/nested/AcmeProperties.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
externalizedconfiguration
.
validate
.
nested
;
//tag::code[]
import
java.net.InetAddress
;
import
javax.validation.Valid
;
import
javax.validation.constraints.NotEmpty
;
import
javax.validation.constraints.NotNull
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.validation.annotation.Validated
;
@ConfigurationProperties
(
prefix
=
"acme"
)
@Validated
public
class
AcmeProperties
{
@NotNull
private
InetAddress
remoteAddress
;
@Valid
private
final
Security
security
=
new
Security
();
public
InetAddress
getRemoteAddress
()
{
return
this
.
remoteAddress
;
}
public
void
setRemoteAddress
(
InetAddress
remoteAddress
)
{
this
.
remoteAddress
=
remoteAddress
;
}
public
Security
getSecurity
()
{
return
this
.
security
;
}
public
static
class
Security
{
@NotEmpty
private
String
username
;
public
String
getUsername
()
{
return
this
.
username
;
}
public
void
setUsername
(
String
username
)
{
this
.
username
=
username
;
}
}
}
// end::code[]
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/profiles/ProductionConfiguration.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
profiles
;
//tag::code[]
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Profile
;
@Configuration
(
proxyBeanMethods
=
false
)
@Profile
(
"production"
)
public
class
ProductionConfiguration
{
// ...
}
// end::code[]
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webapplications/HttpMessageConvertersConfiguration.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
webapplications
;
//tag::code[]
import
java.io.IOException
;
import
org.springframework.boot.autoconfigure.http.HttpMessageConverters
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.HttpInputMessage
;
import
org.springframework.http.HttpOutputMessage
;
import
org.springframework.http.converter.AbstractHttpMessageConverter
;
import
org.springframework.http.converter.HttpMessageConverter
;
import
org.springframework.http.converter.HttpMessageNotReadableException
;
import
org.springframework.http.converter.HttpMessageNotWritableException
;
@Configuration
(
proxyBeanMethods
=
false
)
public
class
HttpMessageConvertersConfiguration
{
@Bean
public
HttpMessageConverters
customConverters
()
{
HttpMessageConverter
<?>
additional
=
new
AdditionalHttpMessageConverter
();
HttpMessageConverter
<?>
another
=
new
AnotherHttpMessageConverter
();
return
new
HttpMessageConverters
(
additional
,
another
);
}
}
// end::code[]
class
AdditionalHttpMessageConverter
extends
AbstractHttpMessageConverter
<
Object
>
{
@Override
protected
boolean
supports
(
Class
<?>
clazz
)
{
return
false
;
}
@Override
protected
Object
readInternal
(
Class
<?>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
,
HttpMessageNotReadableException
{
return
null
;
}
@Override
protected
void
writeInternal
(
Object
t
,
HttpOutputMessage
outputMessage
)
throws
IOException
,
HttpMessageNotWritableException
{
}
}
class
AnotherHttpMessageConverter
extends
AdditionalHttpMessageConverter
{
}
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webapplications/json/MyJsonComponent.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
webapplications
.
json
;
//tag::code[]
import
java.io.IOException
;
import
com.fasterxml.jackson.core.JsonGenerator
;
import
com.fasterxml.jackson.core.JsonParser
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.core.ObjectCodec
;
import
com.fasterxml.jackson.databind.DeserializationContext
;
import
com.fasterxml.jackson.databind.JsonDeserializer
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.JsonSerializer
;
import
com.fasterxml.jackson.databind.SerializerProvider
;
import
org.springframework.boot.jackson.JsonComponent
;
@JsonComponent
public
class
MyJsonComponent
{
public
static
class
Serializer
extends
JsonSerializer
<
MyObject
>
{
@Override
public
void
serialize
(
MyObject
value
,
JsonGenerator
jgen
,
SerializerProvider
serializers
)
throws
IOException
{
jgen
.
writeStringField
(
"name"
,
value
.
getName
());
jgen
.
writeNumberField
(
"age"
,
value
.
getAge
());
}
}
public
static
class
Deserializer
extends
JsonDeserializer
<
MyObject
>
{
@Override
public
MyObject
deserialize
(
JsonParser
jsonParser
,
DeserializationContext
ctxt
)
throws
IOException
,
JsonProcessingException
{
ObjectCodec
codec
=
jsonParser
.
getCodec
();
JsonNode
tree
=
codec
.
readTree
(
jsonParser
);
String
name
=
tree
.
get
(
"name"
).
textValue
();
int
age
=
tree
.
get
(
"age"
).
intValue
();
return
new
MyObject
(
name
,
age
);
}
}
}
// end::code[]
class
MyObject
{
MyObject
(
String
name
,
int
age
)
{
}
String
getName
()
{
return
null
;
}
Integer
getAge
()
{
return
null
;
}
}
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webapplications/json/object/MyJsonComponent.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
webapplications
.
json
.
object
;
//tag::code[]
import
java.io.IOException
;
import
com.fasterxml.jackson.core.JsonGenerator
;
import
com.fasterxml.jackson.core.JsonParser
;
import
com.fasterxml.jackson.core.ObjectCodec
;
import
com.fasterxml.jackson.databind.DeserializationContext
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.SerializerProvider
;
import
org.springframework.boot.jackson.JsonComponent
;
import
org.springframework.boot.jackson.JsonObjectDeserializer
;
import
org.springframework.boot.jackson.JsonObjectSerializer
;
@JsonComponent
public
class
MyJsonComponent
{
public
static
class
Serializer
extends
JsonObjectSerializer
<
MyObject
>
{
@Override
protected
void
serializeObject
(
MyObject
value
,
JsonGenerator
jgen
,
SerializerProvider
provider
)
throws
IOException
{
jgen
.
writeStringField
(
"name"
,
value
.
getName
());
jgen
.
writeNumberField
(
"age"
,
value
.
getAge
());
}
}
public
static
class
Deserializer
extends
JsonObjectDeserializer
<
MyObject
>
{
@Override
protected
MyObject
deserializeObject
(
JsonParser
jsonParser
,
DeserializationContext
context
,
ObjectCodec
codec
,
JsonNode
tree
)
throws
IOException
{
String
name
=
nullSafeValue
(
tree
.
get
(
"name"
),
String
.
class
);
int
age
=
nullSafeValue
(
tree
.
get
(
"age"
),
Integer
.
class
);
return
new
MyObject
(
name
,
age
);
}
}
}
// end::code[]
class
MyObject
{
MyObject
(
String
name
,
int
age
)
{
}
String
getName
()
{
return
null
;
}
Integer
getAge
()
{
return
null
;
}
}
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webapplications/servlet/CorsConfiguration.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
webapplications
.
servlet
;
//tag::code[]
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.servlet.config.annotation.CorsRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
@Configuration
(
proxyBeanMethods
=
false
)
public
class
CorsConfiguration
{
@Bean
public
WebMvcConfigurer
corsConfigurer
()
{
return
new
WebMvcConfigurer
()
{
@Override
public
void
addCorsMappings
(
CorsRegistry
registry
)
{
registry
.
addMapping
(
"/api/**"
);
}
};
}
}
// end::code[]
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webapplications/servlet/ErrorPageConfiguration.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
webapplications
.
servlet
;
//tag::code[]
import
org.springframework.boot.web.server.ErrorPage
;
import
org.springframework.boot.web.server.ErrorPageRegistrar
;
import
org.springframework.boot.web.server.ErrorPageRegistry
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.HttpStatus
;
@Configuration
public
class
ErrorPageConfiguration
{
@Bean
public
ErrorPageRegistrar
errorPageRegistrar
()
{
return
this
::
registerErrorPages
;
}
private
void
registerErrorPages
(
ErrorPageRegistry
registry
)
{
registry
.
addErrorPages
(
new
ErrorPage
(
HttpStatus
.
BAD_REQUEST
,
"/400"
));
}
}
// end::code[]
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webapplications/servlet/MyControllerAdvice.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
webapplications
.
servlet
;
//tag::code[]
import
javax.servlet.RequestDispatcher
;
import
javax.servlet.http.HttpServletRequest
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
;
@ControllerAdvice
(
basePackageClasses
=
AcmeController
.
class
)
public
class
MyControllerAdvice
extends
ResponseEntityExceptionHandler
{
@ResponseBody
@ExceptionHandler
(
MyException
.
class
)
ResponseEntity
<?>
handleControllerException
(
HttpServletRequest
request
,
Throwable
ex
)
{
HttpStatus
status
=
getStatus
(
request
);
return
new
ResponseEntity
<>(
new
MyErrorBody
(
status
.
value
(),
ex
.
getMessage
()),
status
);
}
private
HttpStatus
getStatus
(
HttpServletRequest
request
)
{
Integer
code
=
(
Integer
)
request
.
getAttribute
(
RequestDispatcher
.
ERROR_STATUS_CODE
);
HttpStatus
status
=
HttpStatus
.
resolve
(
code
);
return
(
status
!=
null
)
?
status
:
HttpStatus
.
INTERNAL_SERVER_ERROR
;
}
}
// end::code[]
class
AcmeController
{
}
class
MyException
extends
RuntimeException
{
}
class
MyErrorBody
{
MyErrorBody
(
int
value
,
String
message
)
{
}
}
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webapplications/servlet/MyErrorViewResolver.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
webapplications
.
servlet
;
//tag::code[]
import
java.util.Map
;
import
javax.servlet.http.HttpServletRequest
;
import
org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.servlet.ModelAndView
;
public
class
MyErrorViewResolver
implements
ErrorViewResolver
{
@Override
public
ModelAndView
resolveErrorView
(
HttpServletRequest
request
,
HttpStatus
status
,
Map
<
String
,
Object
>
model
)
{
// Use the request or status to optionally return a ModelAndView
if
(
status
==
HttpStatus
.
INSUFFICIENT_STORAGE
)
{
// We could add custom model values here
new
ModelAndView
(
"myview"
);
}
return
null
;
}
}
// end::code[]
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webapplications/servlet/MyRestController.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
webapplications
.
servlet
;
//tag::code[]
import
java.util.List
;
import
org.springframework.data.repository.CrudRepository
;
import
org.springframework.web.bind.annotation.DeleteMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
@RestController
@RequestMapping
(
"/users"
)
public
class
MyRestController
{
private
final
UserRepository
userRepository
;
private
final
CustomerRepository
customerRepository
;
public
MyRestController
(
UserRepository
userRepository
,
CustomerRepository
customerRepository
)
{
this
.
userRepository
=
userRepository
;
this
.
customerRepository
=
customerRepository
;
}
@GetMapping
(
"/{user}"
)
public
User
getUser
(
@PathVariable
Long
userId
)
{
return
this
.
userRepository
.
findById
(
userId
).
get
();
}
@GetMapping
(
"/{user}/customers"
)
List
<
Customer
>
getUserCustomers
(
@PathVariable
Long
userId
)
{
return
this
.
userRepository
.
findById
(
userId
).
map
(
this
.
customerRepository
::
findByUser
).
get
();
}
@DeleteMapping
(
"/{user}"
)
public
void
deleteUser
(
@PathVariable
Long
userId
)
{
this
.
userRepository
.
deleteById
(
userId
);
}
}
// end::code[]
interface
UserRepository
extends
CrudRepository
<
User
,
Long
>
{
}
interface
CustomerRepository
extends
CrudRepository
<
Customer
,
Long
>
{
List
<
Customer
>
findByUser
(
User
user
);
}
class
User
{
List
<
Customer
>
getCustomers
()
{
return
null
;
}
}
class
Customer
{
}
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webapplications/servlet/ServletFilterConfiguration.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
webapplications
.
servlet
;
//tag::code[]
import
java.io.IOException
;
import
java.util.EnumSet
;
import
javax.servlet.DispatcherType
;
import
javax.servlet.FilterChain
;
import
javax.servlet.ServletException
;
import
javax.servlet.ServletRequest
;
import
javax.servlet.ServletResponse
;
import
org.springframework.boot.web.servlet.FilterRegistrationBean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.filter.GenericFilterBean
;
@Configuration
public
class
ServletFilterConfiguration
{
@Bean
public
FilterRegistrationBean
<
MyFilter
>
myFilter
()
{
FilterRegistrationBean
<
MyFilter
>
registration
=
new
FilterRegistrationBean
<>(
new
MyFilter
());
// ...
registration
.
setDispatcherTypes
(
EnumSet
.
allOf
(
DispatcherType
.
class
));
return
registration
;
}
}
// end::code[]
class
MyFilter
extends
GenericFilterBean
{
@Override
public
void
doFilter
(
ServletRequest
request
,
ServletResponse
response
,
FilterChain
chain
)
throws
IOException
,
ServletException
{
}
}
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webapplications/webflux/CodecConfiguration.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
webapplications
.
webflux
;
//tag::code[]
import
org.springframework.boot.web.codec.CodecCustomizer
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.codec.ServerSentEventHttpMessageReader
;
@Configuration
(
proxyBeanMethods
=
false
)
public
class
CodecConfiguration
{
@Bean
public
CodecCustomizer
myCodecCustomizer
()
{
return
(
configurer
)
->
{
configurer
.
registerDefaults
(
false
);
configurer
.
customCodecs
().
register
(
new
ServerSentEventHttpMessageReader
());
// ...
};
}
}
// end::code[]
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webapplications/webflux/CustomErrorWebExceptionHandler.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
webapplications
.
webflux
;
//tag::code[]
import
reactor.core.publisher.Mono
;
import
org.springframework.boot.autoconfigure.web.WebProperties.Resources
;
import
org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler
;
import
org.springframework.boot.web.reactive.error.ErrorAttributes
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.reactive.function.server.RouterFunction
;
import
org.springframework.web.reactive.function.server.RouterFunctions
;
import
org.springframework.web.reactive.function.server.ServerRequest
;
import
org.springframework.web.reactive.function.server.ServerResponse
;
import
org.springframework.web.reactive.function.server.ServerResponse.BodyBuilder
;
@Component
public
class
CustomErrorWebExceptionHandler
extends
AbstractErrorWebExceptionHandler
{
public
CustomErrorWebExceptionHandler
(
ErrorAttributes
errorAttributes
,
Resources
resources
,
ApplicationContext
applicationContext
)
{
super
(
errorAttributes
,
resources
,
applicationContext
);
}
@Override
protected
RouterFunction
<
ServerResponse
>
getRoutingFunction
(
ErrorAttributes
errorAttributes
)
{
return
RouterFunctions
.
route
(
this
::
acceptsXml
,
this
::
handleErrorAsXml
);
}
private
boolean
acceptsXml
(
ServerRequest
request
)
{
return
request
.
headers
().
accept
().
contains
(
MediaType
.
APPLICATION_XML
);
}
public
Mono
<
ServerResponse
>
handleErrorAsXml
(
ServerRequest
request
)
{
BodyBuilder
builder
=
ServerResponse
.
status
(
HttpStatus
.
INTERNAL_SERVER_ERROR
);
// ... additional builder calls
return
builder
.
build
();
}
}
// end::code[]
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webapplications/webflux/MyRestController.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
webapplications
.
webflux
;
//tag::code[]
import
java.util.List
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
org.springframework.data.repository.reactive.ReactiveCrudRepository
;
import
org.springframework.web.bind.annotation.DeleteMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
@RestController
@RequestMapping
(
"/users"
)
public
class
MyRestController
{
private
final
UserRepository
userRepository
;
private
final
CustomerRepository
customerRepository
;
public
MyRestController
(
UserRepository
userRepository
,
CustomerRepository
customerRepository
)
{
this
.
userRepository
=
userRepository
;
this
.
customerRepository
=
customerRepository
;
}
@GetMapping
(
"/{user}"
)
public
Mono
<
User
>
getUser
(
@PathVariable
Long
userId
)
{
return
this
.
userRepository
.
findById
(
userId
);
}
@GetMapping
(
"/{user}/customers"
)
Flux
<
Customer
>
getUserCustomers
(
@PathVariable
Long
userId
)
{
return
this
.
userRepository
.
findById
(
userId
).
flatMapMany
(
this
.
customerRepository
::
findByUser
);
}
@DeleteMapping
(
"/{user}"
)
public
void
deleteUser
(
@PathVariable
Long
userId
)
{
this
.
userRepository
.
deleteById
(
userId
);
}
}
// end::code[]
interface
UserRepository
extends
ReactiveCrudRepository
<
User
,
Long
>
{
}
interface
CustomerRepository
extends
ReactiveCrudRepository
<
Customer
,
Long
>
{
Flux
<
Customer
>
findByUser
(
User
user
);
}
class
User
{
List
<
Customer
>
getCustomers
()
{
return
null
;
}
}
class
Customer
{
}
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webapplications/webflux/fn/RoutingConfiguration.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
webapplications
.
webflux
.
fn
;
//tag::code[]
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.MediaType
;
import
org.springframework.web.reactive.function.server.RequestPredicate
;
import
org.springframework.web.reactive.function.server.RouterFunction
;
import
org.springframework.web.reactive.function.server.ServerResponse
;
import
static
org
.
springframework
.
web
.
reactive
.
function
.
server
.
RequestPredicates
.
DELETE
;
import
static
org
.
springframework
.
web
.
reactive
.
function
.
server
.
RequestPredicates
.
GET
;
import
static
org
.
springframework
.
web
.
reactive
.
function
.
server
.
RequestPredicates
.
accept
;
import
static
org
.
springframework
.
web
.
reactive
.
function
.
server
.
RouterFunctions
.
route
;
@Configuration
(
proxyBeanMethods
=
false
)
public
class
RoutingConfiguration
{
private
static
final
RequestPredicate
ACCEPT_JSON
=
accept
(
MediaType
.
APPLICATION_JSON
);
@Bean
public
RouterFunction
<
ServerResponse
>
monoRouterFunction
(
UserHandler
userHandler
)
{
// @formatter:off
return
route
(
GET
(
"/{user}"
).
and
(
ACCEPT_JSON
),
userHandler:
:
getUser
).
andRoute
(
GET
(
"/{user}/customers"
).
and
(
ACCEPT_JSON
),
userHandler:
:
getUserCustomers
).
andRoute
(
DELETE
(
"/{user}"
).
and
(
ACCEPT_JSON
),
userHandler:
:
deleteUser
);
// @formatter:on
}
}
// end::code[]
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webapplications/webflux/fn/UserHandler.java
0 → 100644
View file @
23ebf017
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
docs
.
springbootfeatures
.
webapplications
.
webflux
.
fn
;
//tag::code[]
import
reactor.core.publisher.Mono
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.reactive.function.server.ServerRequest
;
import
org.springframework.web.reactive.function.server.ServerResponse
;
@Component
public
class
UserHandler
{
public
Mono
<
ServerResponse
>
getUser
(
ServerRequest
request
)
{
// ...
return
ServerResponse
.
ok
().
build
();
}
public
Mono
<
ServerResponse
>
getUserCustomers
(
ServerRequest
request
)
{
// ...
return
ServerResponse
.
ok
().
build
();
}
public
Mono
<
ServerResponse
>
deleteUser
(
ServerRequest
request
)
{
// ...
return
ServerResponse
.
ok
().
build
();
}
}
// end::code[]
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