This controller
#GetMapping("temp")
public String temp(#RequestParam(value = "foo") int foo,
#RequestParam(value = "bar") Map<String, String> bar) {
return "Hello";
}
Produces the following error:
{
"exception": "org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException",
"message": "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"
}
What I want is to pass some JSON with bar parameter:
http://localhost:8089/temp?foo=7&bar=%7B%22a%22%3A%22b%22%7D, where foo is 7 and bar is {"a":"b"}
Why is Spring not able to do this simple conversion? Note that it works if the map is used as a #RequestBody of a POST request.
Here is the solution that worked:
Just define a custom converter from String to Map as a #Component. Then it will be registered automatically:
#Component
public class StringToMapConverter implements Converter<String, Map<String, String>> {
#Override
public Map<String, Object> convert(String source) {
try {
return new ObjectMapper().readValue(source, new TypeReference<Map<String, String>>() {});
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
}
If you want to use Map<String, String> you have to do the following:
#GetMapping("temp")
public String temp(#RequestParam Map<String, String> blah) {
System.out.println(blah.get("a"));
return "Hello";
}
And the URL for this is: http://localhost:8080/temp?a=b
With Map<String, String>you will have access to all your URL provided Request Params, so you can add ?c=d and access the value in your controller with blah.get("c");
For more information have a look at: http://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/spring-mvc-request-param/ at section Using Map with #RequestParam for multiple params
Update 1: If you want to pass a JSON as String you can try the following:
If you want to map the JSON you need to define a corresponding Java Object, so for your example try it with the entity:
public class YourObject {
private String a;
// getter, setter and NoArgsConstructor
}
Then make use of Jackson's ObjectMapper to map the JSON string to a Java entity:
#GetMapping("temp")
public String temp(#RequestParam Map<String, String> blah) {
YourObject yourObject =
new ObjectMapper().readValue(blah.get("bar"),
YourObject.class);
return "Hello";
}
For further information/different approaches have a look at: JSON parameter in spring MVC controller
Related
I'm trying to find a more or less elegant way to handle PATCH http operations in Spring MVC.
Basically, I'd like to perform a "dual" Jackson deserialization of a JSON document from a Request Body: one to a Map, and the other to the target POJO. Ideally, I would like to perform this in a single PartialDto<T> instance, where T is my target DTO type.
Better giving an example. Let's say I currently have this PUT mapping in a REST Controller:
#PutMapping("/resource")
public MyDto updateWhole(#RequestBody MyDto dto) {
System.out.println("PUT: updating the whole object to " + dto);
return dto;
}
My idea is to build a PartialDto type that would provide both POJO representation of the request body, as well as the Map representation, like this:
#PatchMapping("/resource")
public MyDto updatePartial(#RequestBody PartialDto<MyDto> partial) {
System.out.println("PATCH: partial update of the object to " + partial);
final MyDto dto = partial.asDto();
// Do stuff using the deserialized POJO
final Map<String, Object> map = partial.asMap();
// Do stuff as a deserialized map...
return dto;
}
I hope this will allow me to further expand the PartialDto implementation so I can perform things like this:
if (partial.hasAttribute("myAttribute")) {
final String myAttribute = dto.getMyAttribute();
// ...
}
Or even using a metamodel generator:
if (partial.hasAttribute(MyDto_.myAttribute)) {
final String myAttribute = dto.getMyAttribute();
// ...
}
So the question is simple: Jackson can easily map a JSON document to a POJO. It can also easily map a JSON document to a java Map. How can I do both at the same time in a Wrapper object such as my PartialDto?
public class PartialDto<T> {
private final Map<String, Object> map;
private final T dto;
PartialDto(Map<String, Object> map, T dto) {
this.map = map;
this.dto = dto;
}
public T asDto() {
return this.dto;
}
public Map<String, Object> asMap() {
return Collections.unmodifiableMap(this.map);
}
}
I tried to use a GenericConverter like this (that, of course, I registered in Spring MVC's FormatterRegistry):
public class PartialDtoConverter implements GenericConverter {
private final ObjectMapper objectMapper;
public PartialDtoConverter(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
#Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(String.class, PartialDto.class));
}
#Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
final Class<?> targetClazz = targetType.getResolvableType().getGeneric(0).getRawClass();
final Map<String, Object> map;
try {
map = objectMapper.readValue((String) source, Map.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e); // FIXME
}
final Object dto = objectMapper.convertValue(map, targetClazz);
return new PartialDto(map, dto) ;
}
}
And this converter works well when tested directly using Spring's ConversionService:
#SpringBootTest
class ConverterTest {
#Autowired
private ConversionService conversionService;
#Test
public void testPartialUpdate() throws Exception {
final MyDto dto = new MyDto()
.setIt("It");
final PartialDto<MyDto> partialDto = (PartialDto<MyDto>) conversionService.convert(
"{ \"it\": \"Plop\" }",
new TypeDescriptor(ResolvableType.forClass(String.class), null, null),
new TypeDescriptor(ResolvableType.forClassWithGenerics(PartialDto.class, MyDto.class), null, null)
);
Assertions.assertEquals("Plop", partialDto.asDto().getIt());
Assertions.assertEquals("Plop", partialDto.asMap().get("it"));
}
}
However, it doesn't seem to work in a #RequestBody such as shown above. Reminder:
#PatchMapping("/resource")
public MyDto updatePartial(#RequestBody PartialDto<MyDto> partial) {
// ...
}
Any idea is welcome.
Spring cloud openFeign can't create dynamic query parameters. It throws below exception because SpringMvcContract tries to find the RequestParam value attribute which doesn't exist.
java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0
#RequestMapping(method = RequestMethod.GET, value = "/orders")
Pageable<Order> searchOrder2(#RequestParam CustomObject customObject);
I tried using #QueryMap instead of #RequestParam but #QueryMap does not generate query parameters.
Btw #RequestParam Map<String, Object> params method parameter works fine to generate a dynamic query parameter.
But I want to use a custom object in which the feign client can generate dynamic query parameters from the object's attributes.
From Spring Cloud OpenFeign Docs:
Spring Cloud OpenFeign provides an equivalent #SpringQueryMap annotation, which is used to annotate a POJO or Map parameter as a query parameter map
So your code should be:
#RequestMapping(method = RequestMethod.GET, value = "/orders")
Pageable<Order> searchOrder2(#SpringQueryMap #ModelAttribute CustomObject customObject);
spring-cloud-starter-feign has a open issue for supporting pojo object as request parameter. Therefore I used a request interceptor that take object from feign method and create query part of url from its fields. Thanks to #charlesvhe
public class DynamicQueryRequestInterceptor implements RequestInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(DynamicQueryRequestInterceptor.class);
private static final String EMPTY = "";
#Autowired
private ObjectMapper objectMapper;
#Override
public void apply(RequestTemplate template) {
if ("GET".equals(template.method()) && Objects.nonNull(template.body())) {
try {
JsonNode jsonNode = objectMapper.readTree(template.body());
template.body(null);
Map<String, Collection<String>> queries = new HashMap<>();
buildQuery(jsonNode, EMPTY, queries);
template.queries(queries);
} catch (IOException e) {
LOGGER.error("IOException occurred while try to create http query");
}
}
}
private void buildQuery(JsonNode jsonNode, String path, Map<String, Collection<String>> queries) {
if (!jsonNode.isContainerNode()) {
if (jsonNode.isNull()) {
return;
}
Collection<String> values = queries.computeIfAbsent(path, k -> new ArrayList<>());
values.add(jsonNode.asText());
return;
}
if (jsonNode.isArray()) {
Iterator<JsonNode> it = jsonNode.elements();
while (it.hasNext()) {
buildQuery(it.next(), path, queries);
}
} else {
Iterator<Map.Entry<String, JsonNode>> it = jsonNode.fields();
while (it.hasNext()) {
Map.Entry<String, JsonNode> entry = it.next();
if (StringUtils.hasText(path)) {
buildQuery(entry.getValue(), path + "." + entry.getKey(), queries);
} else {
buildQuery(entry.getValue(), entry.getKey(), queries);
}
}
}
}
}
I have the following class that I want to save and query from MongoDB. I managed to set a custom convert that converts from InstanceType to String and String to InstanceType. InstanceType is a custom enum that will be stored in a different way!
#Document
public class Instance {
private String name;
private InstanceType type;
private List<Configuration> configurations;
private Map<String, String> properties;
}
MongoConfiguration class
#Bean
#Override
public CustomConversions customConversions() {
List<Converter<?, ?>> converters = new ArrayList<>();
converters.add(new InstanceTypeToStringConverter());
converters.add(new StringToInstanceTypeConverter());
return new CustomConversions(converters);
}
#Bean
#Override
public MappingMongoConverter mappingMongoConverter() throws Exception {
MongoMappingContext mappingContext = new MongoMappingContext();
DbRefResolver databaseResolver = new DefaultDbRefResolver(mongoFactory());
MappingMongoConverter mongoConverter = new MappingMongoConverter(databaseResolver, mappingContext);
mongoConverter.setCustomConversions(customConversions());
mongoConverter.afterPropertiesSet();
return mongoConverter;
}
The problem I'm having is that when I try to query the Instance object in MongoDB, Spring uses StringToInstanceType to convert "name" attribute. Why Spring is using this converter? This converter should not only be applied to "type" attribute?
This is the query I'm trying to execute:
public Instance getInstance(InstanceType type, String name) {
return mongoTemplate.findOne(Query.query(
Criteria.where("type").is(type)
.and("name").is(name)
), Instance.class, COLLECTION_INSTANCES);
}
Type attribute is converted using InstanceTypeToStringConverter (Thats OK because the source is InstanceType and target is String)
Name attribute is converted using StringToInstanceTypeConverter (It's wrong because the source is String and target is a String)
Because os this behavior, the query is returning NULL.
Let's say I have the follow object in MongoDB:
{"type": "user", "name": "Sandro"}
InstanceType.USER is converted to "user" (InstanceTypeToStringConverter).
"Sandro" is converted to null (StringToInstanceTypeConverter) because InstanceType.getInstanceType("Sandro") returns null.
In this case Spring will query mongo this way:
Where type = "user" and name = null
How I can pass a Map parameter as a GET param in url to Spring REST controller ?
It’s possible to bind all request parameters in a Map just by adding a Map object after the annotation:
#RequestMapping("/demo")
public String example(#RequestParam Map<String, String> map){
String apple = map.get("APPLE");//apple
String banana = map.get("BANANA");//banana
return apple + banana;
}
Request
/demo?APPLE=apple&BANANA=banana
Source -- https://reversecoding.net/spring-mvc-requestparam-binding-request-parameters/
There are different ways (but a simple #RequestParam('myMap')Map<String,String> does not work - maybe not true anymore!)
The (IMHO) easiest solution is to use a command object then you could use [key] in the url to specifiy the map key:
#Controller
#RequestMapping("/demo")
public class DemoController {
public static class Command{
private Map<String, String> myMap;
public Map<String, String> getMyMap() {return myMap;}
public void setMyMap(Map<String, String> myMap) {this.myMap = myMap;}
#Override
public String toString() {
return "Command [myMap=" + myMap + "]";
}
}
#RequestMapping(method=RequestMethod.GET)
public ModelAndView helloWorld(Command command) {
System.out.println(command);
return null;
}
}
Request: http://localhost:8080/demo?myMap[line1]=hello&myMap[line2]=world
Output: Command [myMap={line1=hello, line2=world}]
Tested with Spring Boot 1.2.7
I want to use some Map<MyEnum, String> as #RequestParam in my Spring Controller. For now I did the following:
public enum MyEnum {
TESTA("TESTA"),
TESTB("TESTB");
String tag;
// constructor MyEnum(String tag) and toString() method omitted
}
#RequestMapping(value = "", method = RequestMethod.POST)
public void x(#RequestParam Map<MyEnum, String> test) {
System.out.println(test);
if(test != null) {
System.out.println(test.size());
for(Entry<MyEnum, String> e : test.entrySet()) {
System.out.println(e.getKey() + " : " + e.getValue());
}
}
}
This acts strange: I just get EVERY Parameter. So if I call the URL with ?FOO=BAR it outputs FOO : BAR. So it definitely takes every String and not just the Strings defined in MyEnum.
So I thought about, why not name the param: #RequestParam(value="foo") Map<MyEnum, String> test. But then I just don't know how to pass the parameters, I always get null.
Or is there any other solution for this?
So if you have a look here: http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html
It says: If the method parameter is Map<String, String> or MultiValueMap<String, String> and a parameter name is not specified [...]. So it must be possible to use value="foo" and somehow set the values ;)
And: If the method parameter type is Map and a request parameter name is specified, then the request parameter value is converted to a Map assuming an appropriate conversion strategy is available. So where to specify a conversion strategy?
Now I've built a custom solution which works:
#RequestMapping(value = "", method = RequestMethod.POST)
public void x(#RequestParam Map<String, String> all) {
Map<MyEnum, String> test = new HashMap<MyEnum, String>();
for(Entry<String, String> e : all.entrySet()) {
for(MyEnum m : MyEnum.values()) {
if(m.toString().equals(e.getKey())) {
test.put(m, e.getValue());
}
}
}
System.out.println(test);
if(test != null) {
System.out.println(test.size());
for(Entry<MyEnum, String> e : test.entrySet()) {
System.out.println(e.getKey() + " : " + e.getValue());
}
}
}
Would be surely nicer if Spring could handle this...
#RequestParam(value="foo") Map<MyEnum, String>
For Above to work:-
You have to pass values in below format
foo[MyTestA]= bar
foo[MyTestB]= bar2
Now to bind String such as "MyTestA","MyTestB" etc..to your MyEnum
You have to define a converter . Take a look a this link