Difference between revisions of "Spring tips"

From DarkWiki
Jump to: navigation, search
(Pageable)
Line 49: Line 49:
 
<source lang="java">
 
<source lang="java">
 
     @GetMapping("/api/test/pagination")
 
     @GetMapping("/api/test/pagination")
     public Page<String> pagination(@PageableDefault(sort = "name",direction = Sort.Direction.ASC) Pageable pageable) {
+
     public Page<String> pager(@PageableDefault(sort = "name",direction = Sort.Direction.ASC) Pageable pageable) {
 
         return new PageImpl<>(List.of("test"),pageable,1);
 
         return new PageImpl<>(List.of("test"),pageable,1);
 
     }
 
     }

Revision as of 11:51, 2 April 2020

Configuration

@Value

Simple setting of values.

    @Value("${prop.strName:default value}}")
    private String localDir;

    @Value("${prop.intName:14}}")
    private int localDir;

Apply a setting, defaulting to a subdirectory of the temporary folder.

    @Value("${prop.name:#{systemProperties['java.io.tmpdir']+'/subdir'}}")
    private String localDir;

When a value is not present in the application.properties file, assume null.

    @Value("${prop.name:#{null}}")
    private String remoteUrl;

Noteworthy classes

Class Notes
ClassPathScanningCandidateComponentProvider Used for scanning for annotated classes etc. See: https://gist.github.com/skempken/dbb2ad55d213cd6a1f50
SpringDataWebProperties A good example of how Spring configuration properties are bound from YML (etc.)

Further reading

A list of configuration properties can be found here: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

Data

Pageable

Given an endpoint:

    @GetMapping("/api/test/pagination")
    public Page<String> pager(@PageableDefault(sort = "name",direction = Sort.Direction.ASC) Pageable pageable) {
        return new PageImpl<>(List.of("test"),pageable,1);
    }

It can be called using:

http://localhost:8080/api/test/pagination?sort=andy,desc&sort=age,asc