Kihagyás

REST folytatás

Ismétlés

Emlékeztetőül, fussuk át a legutóbbi projektünket!

  • Resource-ok
    • csekkoljok mit tettünk láthatóvá és mit rejtettünk el
  • Assembler-ek
    • @Component + @Autowired (opcionális)
    • linkek
  • Frissítsük a RestController-einket ahol és amennyiben szükséges

Spring Data REST

  • dependency:
    1
    2
    3
    4
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-rest</artifactId>
            </dependency>
    
  • application.properties: spring.data.rest.base-path=/api-v2
  • a WebSecurityConfig-ban engedjük ki az api-v2-t (permitAll és csrf ignore)
  • vizsgáljuk meg, hogy milyen eredményt kapunk az api-v2 meghívásakor
    • linkek
    • page, size, sort
    • profile
  • hogyan tudjuk szabályozni, hogy mik jelenjenek meg
    • model osztályon, vagy annak field-jein: @JsonIgnore, @JsonInclude, stb.
    • model osztályon: @RestResource(path = "...", rel = "...")
    • repo-n: @RepositoryRestResource

Microservices

Bővítsük az alkalmazásunkat vCard támogatással!

  • https://github.com/mangstadt/ez-vcard#mavengradle
  • ContactController-ben:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
        @GetMapping("/export/{id}")
        public ResponseEntity<String> exportContact(@PathVariable long id) {
            Contact c = contactService.getContactById(id);
    
            if (c == null) {
                return ResponseEntity.notFound().build();
            }
    
            VCard vcard = new VCard();
            vcard.setFormattedName(c.getName());
    
            String str = Ezvcard.write(vcard).version(VCardVersion.V4_0).go();
    
            return ResponseEntity.ok(str);
        }
    
  • adjunk hozzá további mezőket is (opcionális)

Készítsünk egy alkalmazást, amely az új végpont által előállított tartalmat "fogyasztja el"!

  • új projekt
  • elegendő lesz egy Spring Web, egy DevTools és egy Lombok
  • application.properties: server.port=8081 (vagy vármilyen szabad port, ami nem akad össze a másik alkalmazásunkkal)
  • készítsünk egy TestController-t
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    package com.example.democonsumer.rest;
    
    import ezvcard.Ezvcard;
    import ezvcard.VCard;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    @Slf4j
    public class TestController {
    
        @GetMapping("/test/{id}")
        public void consumeRestApi(@PathVariable long id) {
            RestTemplate restTemplate = new RestTemplate();
    
            try {
                ResponseEntity<String> resp = restTemplate.getForEntity("http://localhost:8080/api/contact/export/{id}", String.class, id);
                log.info("response body=" + resp.getBody());
    
                VCard vcard = Ezvcard.parse(resp.getBody()).first();
    
                log.info("vcard=" + vcard);
            } catch (Exception ex) {
                log.warn(ex.toString());
            }
        }
    
    }
    

Custom Properties

  • application.properties: contact.server.host=http://localhost:8080
  • @Value annotációs megoldás:
    • TestController-ben:
      1
      2
          @Value("${contact.server.host}")
          private String host;
      
  • @ConfigurationProperties-es megoldás:
    • hozzunk létre egy ContactServerProperties osztályt:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      package com.example.democonsumer.config;
      
      import lombok.Data;
      import org.springframework.boot.context.properties.ConfigurationProperties;
      
      @Data
      @Configuration
      @ConfigurationProperties(prefix = "contact.server")
      public class ContactServerProperties {
      
          private String host;
      
      }
      
    • ezután a TestController osztályban a szokásos @Autowired módszerrel tudjuk használni

Ne aggódjunk, ha az IDE nem még így sem akarja felismerni a property-nket!

Videó

A gyakorlat anyagáról készült videó:

SUAF_08_gyak

További hasznos anyagok


Utolsó frissítés: 2021-11-03 22:31:33
Back to top