Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Upgrade from Spring Boot 1.5 to 2.0

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

POM1.5 org.springframework.boot spring-boot-starter-parent 1.5.10.RELEASE 2.0 org.springframework.boot spring-boot-starter-parent 2.0.7.RELEASE Spring Data

Some of Spring Data's methods have been renamed:

FindOne (id)-> findById (id) (Optional returned) delete (id)-> deleteById (id) exists (id)-> existsById (id) findAll (ids)-> findAllById (ids) configuration

When upgrading, you can add spring-boot-properties-migrator to pom so that you will be prompted for the configuration that needs to be changed at startup:

Org.springframework.boot spring-boot-properties-migrator runtime

Among them, the major changes are security (The security auto-configuration is no longer customizable,A global security auto-configuration is now provided), management, banner, server and so on.

Security

The configuration and management.security at the beginning of security have expired. If the following configurations are no longer supported, you need to adjust them to the code:

Security: ignored: / api-docs,/swagger-resources/**,/swagger-ui.html**,/webjars/**managementmanagement: security: enabled: false port: 8090

Modified to:

Management: server: port: 8090datasourcedatasource: initialize: false

Modified to:

Datasource: initialization-mode: never

If you use PostgreSQL, you may get an error: Method org.postgresql.jdbc.PgConnection.createClob () is not yet implemented hibernate. You need to modify the configuration:

Jpa: database-platform: org.hibernate.dialect.PostgreSQLDialect properties: hibernate: default_schema: test jdbc: lob: non_contextual_creation: truebanner adjust to spring.bannerserver adjust to server.servlet

For more parameters that need to be adjusted, please see the reference documentation at the end of the article.

Security

WebSecurityConfigurerAdapter

Security.ignored@Overridepublic void configure (WebSecurity web) throws Exception {web.ignoring () .antMatchers ("/ api-docs", "/ swagger-resources/**", "/ swagger-ui.html**", "/ webjars/**");} AuthenticationManager

Such as injecting AuthenticationManager into the code

@ Autowiredprivate AuthenticationManager authenticationManager

An error will be reported at startup: Field authenticationManager required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found. Please add the following code to WebSecurityConfigurerAdapter:

@ Bean@Overridepublic AuthenticationManager authenticationManagerBean () throws Exception {return super.authenticationManagerBean ();} Actuator configuration property change Old propertyNew propertyendpoints.id.*management.endpoint.id.*endpoints.cors.*management.endpoints.web.cors.*endpoints.jmx.*management.endpoints.jmx.*management.addressmanagement.server.addressmanagement.context-pathmanagement.server.servlet.context-pathmanagement.ssl.*management.server.ssl.*management.portmanagement.server.port

The default value of management.endpoints.web.base-path is / actuator, that is, actuator ([/ actuator/health], [/ actuator/info]) is added to the front of the Actuator access path, which can be seen in the startup log.

Since management.security is no longer supported, permission configuration needs to be added to WebSecurityConfigurerAdapter:

.authorizeRequests (). RequestMatchers (EndpointRequest.to ("health", "info"). PermitAll () Endpoint change EndpointChanges/actuatorNo longer available. There is, however, a mapping available at the root of management.endpoints.web.base-path that provides links to all the exposed endpoints./auditeventsThe after parameter is no longer required/autoconfigRenamed to / conditions/docsNo longer available (the API documentation is part of the published documentation now) / healthRather than relying on the sensitive flag to figure out if the health endpoint had to show full details or not, there is now a management.endpoint.health.show-details option: never, always, when-authorized. By default, / actuator/health is exposed and details are not shown./traceRenamed to / httptrace New feature Configuration Property Binding

You can use Binder API directly in your code to read from the configuration file:

Public class Person implements EnvironmentAware {private Environment environment; @ Override public void setEnvironment (Environment environment) {this.environment = environment;} public void bind () {List people = Binder.get (environment) .bind ("my.property", Bindable.listOf (PersonName.class)) .orElseThrow (IllegalStateException::new);}}

YAML configuration

My: property:-first-name: Jane last-name: Doe-first-name: John last-name: DoeSpring Data Web Configuration

Add spring.data.web configuration to set paging and sorting:

# DATA WEB (SpringDataWebProperties) spring.data.web.pageable.default-page-size=20 # Default page size.spring.data.web.pageable.max-page-size=2000 # Maximum page size to be accepted.spring.data.web.pageable.one-indexed-parameters=false # Whether to expose and assume 1-based page number indexes.spring.data.web.pageable.page-parameter=page # Page index parameter name.spring.data.web.pageable.prefix= # General prefix to be prepended to the page number and page size parameters.spring.data.web.pageable. Qualifier-delimiter=_ # Delimiter to be used between the qualifier and the actual page number and size properties.spring.data.web.pageable.size-parameter=size # Page size parameter name.spring.data.web.sort.sort-parameter=sort # Sort parameter name. Support custom JdbcTemplate# JDBC (JdbcProperties) spring.jdbc.template.fetch-size=-1 # Number of rows that should be fetched from the database when more rows are needed.spring.jdbc.template.max-rows=-1 # Maximum number of rows.spring.jdbc.template.query-timeout= # Query timeout. Default is to use the JDBC driver's default configuration. If a duration suffix is not specified, seconds will be used. Support for hibernate custom naming policy Reactive

See Release Notes for more new features.

Swagger io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2

Swagger 2.9added handling of the @ ApiParam property example, which is displayed in Swagger UI and documentation, so be careful to set the appropriate example value:

@ ApiOperation (value = "Delete airline by id") @ GetMapping ("/ airlines/delete/ {airlineId}") public void deleteAirline (@ ApiParam (required = true, example = "123") @ PathVariable Long airlineId)

Otherwise you will see the Warn log:

AbstractSerializableParameter

@ JsonProperty ("x-example") public Object getExample () {if (example = = null) {return null;} try {if (BaseIntegerProperty.TYPE.equals (type)) {return Long.valueOf (example);} else if (DecimalProperty.TYPE.equals (type)) {return Double.valueOf (example) } else if (BooleanProperty.TYPE.equals (type)) {if ("true" .equalsIgnoreCase (example) | | "false" .equalsIgnoreCase (defaultValue)) {return Boolean.valueOf (example);} catch (NumberFormatException e) {LOGGER.warn (String.format ("Illegal DefaultValue% Ignores", defaultValue, type), e);} return example;}

In addition, Swagger 2.9.2 automatically adds @ ApiImplicitParams to org.springframework.data.domain.Pageable.

Reference documentation

Spring Boot Reference Guide 2.0.7.RELEASE

Spring Boot 2.0 Migration Guide

Spring Boot 2.0 Configuration Changelog

Spring Boot 2.0 Release Notes

Spring Boot Relaxed Binding 2.0

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report