Spring ArgumentResolver to get pathvariable map and - java

this my method signature
#RequestMapping(value = {"/article", "/article/{id}", "/article/{name}"}, method = RequestMethod.GET,
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<JsonNode> get(#PathVariable Map<String, String> pathVarsMap, #RequestParam(value="test") MultiValueMap<String, String> test, #RequestBody(required=false) JsonNode requestBody )
I want to make this into
public ResponseEntity<JsonNode> get( MyStructure mystr)
where MyStructure will have #PathVariable Map<String, String> pathVarsMap, #RequestParam(value="test") MultiValueMap<String, String> test, #RequestBody(required=false) JsonNode requestBody inside of it.
I know that I have to use custom resolvers and implement resolveArgument. One of the examples i saw did (Map<String, String>) httpServletRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE). But im not sure how to get it to work. Can i create MultiValueMap and RequestBody inside MyString ?
In another place, I see that the recommendation is to use
#Nonnull
protected final Map<String, String> getUriTemplateVariables(NativeWebRequest request) {
#SuppressWarnings("unchecked")
Map<String, String> variables =
(Map<String, String>) request.getAttribute(
URI_TEMPLATE_VARIABLES_ATTRIBUTE, SCOPE_REQUEST);
return (variables != null) ? variables : Collections.<String, String>emptyMap();
}
so im a bit confused on how should i be implementing this

All #PathVariable , #RequestParam and #RequestBody can only be annotated on the method parameters , so there are no ways for you to annotate them on the object fields.
The codes of the existing HandlerMethodArgumentResolver that resolve the values for these annotations also assume these annotation are annotated on the method parameters ,that means you also cannot simply delegate to them to resolve the value for your request object.
Your best bet is to simply reference the corresponding HandlerMethodArgumentResolver for each annotation and copy the related codes to your implementation.
For #PathVariable , it is resolved by PathVariableMapMethodArgumentResolver
For #RequestParam on MultiValueMap , it is resolved by RequestParamMapMethodArgumentResolver
For #RequestBody , it is resolved by RequestResponseBodyMethodProcessor . Internally , it works with a list of HttpMessageConverter to read the HTTP request body. As you are now using Jackson to read the request body , you only need to focus on MappingJackson2HttpMessageConverter for simplicity.
It is easier than I expected. There following implementation should be a good starting point for you.
First define MyStructure class :
public class MyStructure {
public Map<String, String> pathVariables;
public MultiValueMap<String, String> queryParameters;
public JsonNode requestBody;
}
And implement MyStructureArgumentResolver :
public class MyStructureArgumentResolver implements HandlerMethodArgumentResolver {
private MappingJackson2HttpMessageConverter messageConverter;
public MyStructureArgumentResolver(MappingJackson2HttpMessageConverter messageConverter) {
super();
this.messageConverter = messageConverter;
}
#Override
public boolean supportsParameter(MethodParameter parameter) {
return MyStructure.class.isAssignableFrom(parameter.getParameterType());
}
#Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
MyStructure request = new MyStructure();
request.queryParameters = resolveQueryParameters(webRequest);
request.pathVariables = resolvePathVariables(webRequest);
request.requestBody = resolveRequestBody(webRequest, parameter);
return request;
}
private MultiValueMap<String, String> resolveQueryParameters(NativeWebRequest webRequest) {
// resolve all query parameter into MultiValueMap
Map<String, String[]> parameterMap = webRequest.getParameterMap();
MultiValueMap<String, String> result = new LinkedMultiValueMap<>(parameterMap.size());
parameterMap.forEach((key, values) -> {
for (String value : values) {
result.add(key, value);
}
});
return result;
}
private Map<String, String> resolvePathVariables(NativeWebRequest webRequest) {
Map<String, String> uriTemplateVars = (Map<String, String>) webRequest.getAttribute(
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
if (!CollectionUtils.isEmpty(uriTemplateVars)) {
return new LinkedHashMap<>(uriTemplateVars);
} else {
return Collections.emptyMap();
}
}
private JsonNode resolveRequestBody(NativeWebRequest webRequest, MethodParameter parameter)
throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {
HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
HttpInputMessage inputMessage = new ServletServerHttpRequest(servletRequest);
MediaType contentType;
try {
contentType = inputMessage.getHeaders().getContentType();
} catch (InvalidMediaTypeException ex) {
throw new HttpMediaTypeNotSupportedException(ex.getMessage());
}
if (contentType == null) {
contentType = MediaType.APPLICATION_OCTET_STREAM;
}
Class<?> contextClass = parameter.getContainingClass();
JsonNode body = JsonNodeFactory.instance.objectNode();
if (messageConverter.canRead(JsonNode.class, contextClass, contentType)) {
body = (JsonNode) messageConverter.read(JsonNode.class, inputMessage);
}
return body;
}
}
Then register MyStructureArgumentResolver :
#Configuration
public class WebConfig implements WebMvcConfigurer {
#Autowired
private MappingJackson2HttpMessageConverter messageConverter;
#Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new MyStructureArgumentResolver(messageConverter));
}
}
And use it in the controller method :
#RequestMapping(value = { "/test/{name}" }, method = RequestMethod.GET)
public ResponseEntity<String> test(MyStructure request) {
}

#PostMapping("/get")
public ResponseEntity<JsonNode> get( #RequestBody MyStructure mystr){...}
When call this api, fill in params in request body, send body as application/json. Refer to this sample: sample project

Related

How Does #ResponseBody annotation binds data on void methods in a controller?

I searched for the use of #ResponseBody annotation and I found that this annotation binds data to the response according to the return type of the method here(https://zetcode.com/springboot/responsebody/),But today I got an old project where I saw a controller method like this:-
#Controller
public class MyConroller {
#Autowired
JsonMapper mapper;
#RequestMapping(value = "/methodURL", method = RequestMethod.GET)
#ResponseBody
public void controllerMethod(HttpServletRequest req, HttpServletResponse res) {
HttpSession hs = req.getSession(false);
Map<String, Object> map = service.getList();
mapper.WritecInJson(res, map);
}
}
JASON Mapper-:
public class JsonMapper {
public void WritecInJson(HttpServletResponse res,Object object)
{
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
MediaType jsonMimeType = MediaType.APPLICATION_JSON;
if(jsonConverter.canWrite(object.getClass(), jsonMimeType)){
jsonConverter.write(object , jsonMimeType, new ServletServerHttpResponse(res));
}
}
As you can see the method in the controller class has void as its return type. So, How #ResponseBody binds data to the response through this void method?
#ResponseBody
must defined a return type. If you really don’t need to define a method with return type. You can use HttpServletResponse.getOutputStream() to write back the response content.

Is it possible to pass custom object into SpringBoot's endpoint?

I have a SpringBoot project and in an every endpoint's the first call is parsing a request header into a POJO object. making sure required headers are set. Is it possible "to teach" SpringBoot to be able to provide custom object in endpoints?
class CommonHeader {
private String callerId;
private String systemId;
....
public static CommonHeader parseCommonHeader(Map<String, String> map) {
CommonHeader header = new CommonHeader();
header.setCallerId(map.get("x-caller-id"));
.....
return header;
}
}
#Path("/columnConfigs")
Response getColumnConfig(#RequestHeader Map<String, String> headers) {
CommonHeader commonHeader = parseCommonHeader(headers);
....
}
#Path("/other")
Response getColumnConfig(#RequestHeader Map<String, String> headers) {
CommonHeader commonHeader = parseCommonHeader(headers);
....
}
I would like to be able to simplify the following code into:
#Path("/columnConfigs")
Response getColumnConfig(CommonHeader commonHeader) {
....
}
#Path("/other")
Response getColumnConfig(CommonHeader commonHeader) {
....
}
I was looking for some solution for this, that is, bind headers to an object of our definition. I found a good technique here: Using Custom Data Binders in SpringMVC
What you have to do in a nutshell are these, apart from the already created CommonHeader class:
Define an annotation, say #MyHeaders (with #Retention( RUNTIME ) and #Target( PARAMETER ))
#Retention( RUNTIME )
#Target( PARAMETER )
public #interface MyHeaders {}
Create an implementation of HandlerMethodArgumentResolver, say MyHeadersResolver. This will map the headers from the request into an instance of CommonHeader and return it.
#Override
public boolean supportsParameter( MethodParameter methodParameter ){
return methodParameter.getParameterAnnotation( MyHeaders.class ) != null;
}
#Override
public Object resolveArgument( MethodParameter methodParameter, ModelAndViewContainer mavc, NativeWebRequest req, WebDataBinderFactory wdbf ) throws Exception{
HttpServletRequest request = (HttpServletRequest) req.getNativeRequest();
CommonHeader h = new CommonHeader();
Collections.list( request.getHeaderNames() ).forEach( header -> {
switch( header.toLowerCase() ) {
//Set values into CommonHeader instance
}
});
return h;
}
Declare this to SpringMVC as an argument resolver in a class that implements WebMvcConfigurer, like this:
#Override
public void addArgumentResolvers( List<HandlerMethodArgumentResolver> resolvers ) {
resolvers.add( new MyHeadersResolver() );
}
Your controller method signature will now look like this:
Response getColumnConfig( #MyHeaders CommonHeader commonHeader )
Define CommonHeader to extend Map<String, String>:
#RequestHeader
public interface CommonHeader extends Map<String, String> {
}

How to add HTTP headers in Microprofile REST client dynamically?

I am developing application, which uses microprofile rest client. And that rest client should send REST request with various http header. Some headers names changes dynamically. My microprofile rest client should be generic, but I did not find how to implement such behaviour.
According to the documentation you need to specify all header names in the implementation via annotations and that is not generic. Is there any way how to "hack" it and add HTTP headers programatically?
Thanks in advance
GenericRestClient genericRestClient = null;
Map<String, Object> appConfig = context.appConfigs();
String baseUrl = (String) appConfig.get("restClient.baseUrl");
path = (String) appConfig.get("restClient.path");
try {
genericRestClient = RestClientBuilder.newBuilder()
.baseUri(new URI(baseUrl)).build(GenericRestClient.class);
}catch(URISyntaxException e){
logger.error("",e);
throw e;
}
Response response = genericRestClient.sendMessage(path, value);
logger.info("Status: "+response.getStatus());
logger.info("Response body: "+response.getEntity().toString());
Generic rest client code:
#RegisterRestClient
public interface GenericRestClient {
#POST
#Path("{path}")
#Produces("application/json")
#Consumes("application/json")
public Response sendMessage(<here should go any map of custom headers>, #PathParam("path") String pathParam, String jsonBody);
}
According to the spec, you can use a ClientHeadersFactory. Something like this:
public class CustomClientHeadersFactory implements ClientHeadersFactory {
#Override public MultivaluedMap<String, String> update(
MultivaluedMap<String, String> incomingHeaders,
MultivaluedMap<String, String> clientOutgoingHeaders
) {
MultivaluedMap<String, String> returnVal = new MultivaluedHashMap<>();
returnVal.putAll(clientOutgoingHeaders);
returnVal.putSingle("MyHeader", "generated");
return returnVal;
}
}
#RegisterRestClient
#RegisterClientHeaders(CustomClientHeadersFactory.class)
public interface GenericRestClient {
...
}
You can't pass values directly to the ClientHeadersFactory; but you can directly access the headers of an incoming request, if your own service is called via JAX-RS. You can also #Inject anything you need. If this is still not sufficient and you really need to pass things from the service call, you can use a custom #RequestScope bean, e.g.:
#RequestScope
class CustomHeader {
private String name;
private String value;
// getters/setters
}
public class CustomClientHeadersFactory implements ClientHeadersFactory {
#Inject CustomHeader customHeader;
#Override public MultivaluedMap<String, String> update(
MultivaluedMap<String, String> incomingHeaders,
MultivaluedMap<String, String> clientOutgoingHeaders
) {
MultivaluedMap<String, String> returnVal = new MultivaluedHashMap<>();
returnVal.putAll(clientOutgoingHeaders);
returnVal.putSingle(customHeader.getName(), customHeader.getValue());
return returnVal;
}
}
class Client {
#Inject CustomHeader customHeader;
void call() {
customHeader.setName("MyHeader");
customHeader.setValue("generated");
...
Response response = genericRestClient.sendMessage(path, value);
}
}
Add ClientRequestHeader to your client as follows:
#POST
// read from application.properties
#ClientRequestHeader(name=“myHeader1”, value="${myProperty}")
// read from a method
#ClientRequestHeader(name=“myHeader2”, value="{addHeaderMethod}")
Response sendMessage();
default String addHeaderMethod() {
//put your logic here
return “my dynamic value”;
}

Map as parameter in RestAPI Post request

I have created an API with a Map<String, Integer> parameter, like this:
#RequestMapping(value = "upload", method = RequestMethod.POST)
public ResponseEntity<String> handleContactsFileUpload(#RequestParam("file") MultipartFile file,
#RequestParam("name") String name,
#RequestParam("campaignAppItemId") Long campaignAppItemId,
#RequestParam("fileColumnHeaders") Map<String,Integer> fileColumnHeaders) throws Exception {
if (file == null)
return new ResponseEntity<>("No file uploaded", HttpStatus.BAD_REQUEST);
contactService.handleContactsFile(file, name, campaignAppItemId,fileColumnHeaders);
return new ResponseEntity<>("File uploaded successfully", HttpStatus.OK);
}
I am trying to call this via Postman:
I passed the fileColumnHeaders inside Body->Form Data as in the screenshot.
Then I got a message like this in Postman:
Failed to convert value of type 'java.lang.String' to required type 'java.util.Map'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.Map': no matching editors or conversion strategy found.
Anybody know why this message came ?
How can we pass a map as a parameter in Rest API request?
How can we pass a map through Postman?
You could use #RequestBody instead of #RequestParam for Maps and other non trivial data types and objects - this way spring will map the JSON representing your map parameter to a domain object, which is then serializable and can be converted to a java object.
... Or simply create a converter:
#Component
#RequiredArgsConstructor
public class StringToMapConverter implements Converter<String, Map<String, Object>> {
private final ObjectMapper objectMapper;
#Override
public Map<String, Object> convert(String source) {
try {
return objectMapper.readValue(source, new TypeReference<Map<String, String>>() {
});
} catch (final IOException e) {
return null;
}
}
}
Firstly, you create DTO object to get all data from your request.
public class FormDataDTO {
private MultipartFile file;
private String name;
private Long campaignAppItemId;
private Map<String,Integer> fileColumnHeaders;
// getters, setters
}
Secondly, you can map FormDataDTO from your request without any annotation:
#RequestMapping(value = "upload", method = RequestMethod.POST)
public ResponseEntity<String> handleContactsFileUpload(FormDataDTO formDataDTO){
// your logic code here
}
Finally, form-data in your request will be:
I think this could work:
#RequestMapping(value = "upload/{fileColumnHeaders}", method = RequestMethod.POST)
public ResponseEntity<String> handleContactsFileUpload(#RequestParam("file") MultipartFile file,
#RequestParam("name") String name,
#RequestParam("campaignAppItemId") Long campaignAppItemId,
#MatrixVariable Map<String,Integer> fileColumnHeaders) throws Exception {
if (file == null)
return new ResponseEntity<>("No file uploaded", HttpStatus.BAD_REQUEST);
contactService.handleContactsFile(file, name, campaignAppItemId,fileColumnHeaders);
return new ResponseEntity<>("File uploaded successfully", HttpStatus.OK);
}
Put all other parameters into the body, but add the fileColumnHeaders to the URL like this:
/upload/firstName=1;lastName=2;address=3;phone=4
You will also need this extra configuration:
#Configuration
public class WebConfig implements WebMvcConfigurer {
#Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}

Spring RequestParam Map<String, String>

I have this in my controller:
#RequestMapping(value = "/myUrl", method = RequestMethod.GET)
public String myUrl(#RequestParam(value = "test") Map<String, String> test)
{
return test.toString();
}
And I'm making this HTTP request:
GET http://localhost:8080/myUrl?test[a]=1&test[b]=2
But in the logs I'm getting this error:
org.springframework.web.bind.MissingServletRequestParameterException: Required Map parameter 'test' is not present
How can I pass Map<String, String> to Spring?
May be it's a bit late but this can be made to work by declaring an intermediate class:
public static class AttributeMap {
private Map<String, String> attrs;
public Map<String, String> getAttrs() {
return attrs;
}
public void setAttrs(Map<String, String> attrs) {
this.attrs = attrs;
}
}
And using it as parameter type in method declaration (w/o #RequestParam):
#RequestMapping(value = "/myUrl", method = RequestMethod.GET)
public String myUrl(AttributeMap test)
Then with a request URL like this:
http://localhost:8080/myUrl?attrs[1]=b&attrs[222]=aaa
In test.attrs map all the attributes will present as expected.
It's not immediately clear what you are trying to do since test[a] and test[b] are completely unrelated query string parameters.
You can simply remove the value attribute of #RequestParam to have your Map parameter contain two entries, like so
{test[b]=2, test[a]=1}

Categories

Resources