How does pathVar Attribute of @MatrixVariable annotation works in Spring?

JAVA

I was reading regarding the @Matrixvariable annotation in Spring doc Spring Doc

I have Understood this simple syntax // GET /pets/42;q=11;r=22

@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET)
public void findPet(@PathVariable String petId, @MatrixVariable int q) {

  // petId == 42
  // q == 11

}

but having problem in understanding the below snippet

// GET /owners/42;q=11;r=12/pets/21;q=22;s=23

@RequestMapping(value = "/owners/{ownerId}/pets/{petId}", method = RequestMethod.GET)
  public void findPet(
        @MatrixVariable Map<String, String> matrixVars,
        @MatrixVariable(pathVar="petId"") Map<String, String> petMatrixVars) {

    // matrixVars: ["q" : [11,22], "r" : 12, "s" : 23]
    // petMatrixVars: ["q" : 11, "s" : 23]

  }

What is this syntax @MatrixVariable(pathVar="petId"") I haven't understood the pathVar attribute of Matrixvariable annotation?

This line is ok for me // matrixVars: ["q" : [11,22], "r" : 12, "s" : 23] that this variable added with all the matrix variables. but how does petMatrixVars added with these Specific values mean

//petMatrixVars: ["q" : 11, "s" : 23]  ? why not  //petMatrixVars: ["q" : 22, "s" : 23]  ?

Thanks in Advance for your time spent on this thread!!

Deadpool

This is called Partial Binding it is used to get all variables from that segment in that path or if you want to get each variable from that path segment docs, and output is wrong in this documentation here

In your example you will get all variables that are in path after petId {21}

// GET /owners/42;q=11;r=12/pets/21;q=22;s=23
 @MatrixVariable(pathVar="petId") Map<String, String> petMatrixVars)

If you want to get only q after petId segment then

@MatrixVariable(value ="q",pathVar="petId") int q

Here is the example with output, for @MatrixVariable we need to enable them first

import org.springframework.context.annotation.Configuration;
importorg.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

 @Configuration
 public class WebConfig implements WebMvcConfigurer {

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
    UrlPathHelper urlPathHelper = new UrlPathHelper();
    urlPathHelper.setRemoveSemicolonContent(false);
    configurer.setUrlPathHelper(urlPathHelper);
  }
}

Controller with @requestmapping method

@RequestMapping(value = "/owners/{ownerId}/pets/{petId}", method = RequestMethod.GET)
  public void findPet(
        @MatrixVariable Map<String, String> matrixVars,
        @MatrixVariable(pathVar="petId") Map<String, String> petMatrixVars) {
    System.out.println(matrixVars);
     System.out.println(petMatrixVars);

  }
}

Request: http://localhost:8080/sample/owners/42;q=11;r=12/pets/21;q=22;s=23

Output:

{q=11, r=12, s=23}
{q=22, s=23}

And if i change @MatrixVariable Map<String, List<String>> matrixVars, the output is

{q=[11, 22], r=[12], s=[23]}
{q=22, s=23}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

@MatrixVariableアノテーションのpathVar属性はSpringでどのように機能しますか?

分類Dev

How @scheduled annotation works in Spring Boot

分類Dev

Java spring annotation attribute

分類Dev

How does the mergeAll works?

分類Dev

How to init custom annotation in spring

分類Dev

How Spring Sidecar Works with Docker

分類Dev

How does RAND() works in BigQuery?

分類Dev

How does ArrayBuffer works in memory?

分類Dev

How does a port listening works?

分類Dev

How nested FETCH JOIN works on Hibernate using JpaRepository and @Query annotation?

分類Dev

how annotation @Repository in java spring work?

分類Dev

After disabling Vertex Attribute Array how glDrawArrays() works?

分類Dev

Spring MVC. Not recognizing RequestMapping annotation name attribute when loading context

分類Dev

How does validity.valid works?

分類Dev

How does the function printf works in C?

分類Dev

How does Bounded typeclass works for custom datatype?

分類Dev

How does the following quicksort method works?

分類Dev

How does publish() with a selector works in Rx?

分類Dev

How does PHP cURL DNS caching works?

分類Dev

How does const_cast works?

分類Dev

How does an iterator works internally in C++?

分類Dev

how does branching works , using the gitflow workflow?

分類Dev

How does this function to create a PowerSet in Python works?

分類Dev

how does append works with for loop in python

分類Dev

How does the the line after the javascript popupwindow works?

分類Dev

How does the RequestAttributes.InternalNetworkAccess works?

分類Dev

How does google map API works?

分類Dev

How the data annotation attribute validation is differ from client side validation in MVC?

分類Dev

kotlin and @Valid Spring annotation

Related 関連記事

  1. 1

    @MatrixVariableアノテーションのpathVar属性はSpringでどのように機能しますか?

  2. 2

    How @scheduled annotation works in Spring Boot

  3. 3

    Java spring annotation attribute

  4. 4

    How does the mergeAll works?

  5. 5

    How to init custom annotation in spring

  6. 6

    How Spring Sidecar Works with Docker

  7. 7

    How does RAND() works in BigQuery?

  8. 8

    How does ArrayBuffer works in memory?

  9. 9

    How does a port listening works?

  10. 10

    How nested FETCH JOIN works on Hibernate using JpaRepository and @Query annotation?

  11. 11

    how annotation @Repository in java spring work?

  12. 12

    After disabling Vertex Attribute Array how glDrawArrays() works?

  13. 13

    Spring MVC. Not recognizing RequestMapping annotation name attribute when loading context

  14. 14

    How does validity.valid works?

  15. 15

    How does the function printf works in C?

  16. 16

    How does Bounded typeclass works for custom datatype?

  17. 17

    How does the following quicksort method works?

  18. 18

    How does publish() with a selector works in Rx?

  19. 19

    How does PHP cURL DNS caching works?

  20. 20

    How does const_cast works?

  21. 21

    How does an iterator works internally in C++?

  22. 22

    how does branching works , using the gitflow workflow?

  23. 23

    How does this function to create a PowerSet in Python works?

  24. 24

    how does append works with for loop in python

  25. 25

    How does the the line after the javascript popupwindow works?

  26. 26

    How does the RequestAttributes.InternalNetworkAccess works?

  27. 27

    How does google map API works?

  28. 28

    How the data annotation attribute validation is differ from client side validation in MVC?

  29. 29

    kotlin and @Valid Spring annotation

ホットタグ

アーカイブ