juanli
juanli

Reputation: 613

How to get specific headers (spring-boot application) and pass to controller?

enter image description here

I have a controller in spring boot. I want to get the formId from Form Data (see the image above). @RequestHeader(value="formId") doesn't work. How to get the value?

Upvotes: 1

Views: 184

Answers (2)

Vimukthi
Vimukthi

Reputation: 880

First you need below dependency,

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1</version>
 </dependency>

Then you can get Form data value using below example code,

@PostMapping("/foo")
@ResponseBody
public ResponseEntity<?> getData(@FormParam("formId") String formId) {
    System.out.println(formId);
}

In here formParam variable name and parameter name should be equal.

Upvotes: 0

Simon Martinelli
Simon Martinelli

Reputation: 36223

formId is not from the header but the from form data which is the request body.

You can get it like in this example:

@GetMapping("foo)
public String foo(@RequestBody MultiValueMap<String, String> formData) {

     String formId = formData.get("formId");
     // your code
}

Upvotes: 1

Related Questions