You can create your controller to support application/json request in many way. And if your request is not match with your controller that request will be fail.
Controller to support JSON request and HEADER variables.
Explaination
Line 21-22: Declare controller mapping to URL and receive only POST method (You can change as your want)
Line 23: Declare controller's method to support URL.
In this example header USER-AGENT will be send to variable userAgent and all json request data will be hold by variable payload. If you want to get your custom header you can change value to your variable as you want or add more parameter in method declaration. In example below will be receive only json request data without header variable.
Look at line 23 again. I just removed @RequestHeader from method's parameters.
Controller to support JSON request and HEADER variables.
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 | package com.myexample; import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class RecognizedReceiver { private static final Logger logger = LoggerFactory.getLogger(RecognizedReceiver.class); @RequestMapping( value = "/MyController", method = RequestMethod.POST) public HashMap<String, Object> MyController(@RequestHeader(value="USER-AGENT") long userAgent, @RequestBody Map<String, Object> payload) { HashMap<String, Object> result = new HashMap<>(); result.put("result", processResult); return result; } } |
Explaination
Line 21-22: Declare controller mapping to URL and receive only POST method (You can change as your want)
Line 23: Declare controller's method to support URL.
In this example header USER-AGENT will be send to variable userAgent and all json request data will be hold by variable payload. If you want to get your custom header you can change value to your variable as you want or add more parameter in method declaration. In example below will be receive only json request data without header variable.
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 | package com.myexample; import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class RecognizedReceiver { private static final Logger logger = LoggerFactory.getLogger(RecognizedReceiver.class); @RequestMapping( value = "/MyController", method = RequestMethod.POST) public HashMap<String, Object> MyController(@RequestBody Map<String, Object> payload) { HashMap<String, Object> result = new HashMap<>(); result.put("result", processResult); return result; } } |
Look at line 23 again. I just removed @RequestHeader from method's parameters.
Comments
Post a Comment