0

I'm using spring 4.0.5.RELEASE and jackson-databind 2.2.3 in my web application. When sending this JSON:

{"keyField1":"57579","keyField2":"sdf","someField":"sdasd","parameters":[{"parameterName":"dfgdfg","parameterValue":"sdf"},{"parameterName":"erwer","parameterValue":"sdfsdf"}]}

to the controller all I get is a HTTP 400 Bad Request at browser, I don't see any error at local websphere log, but after some tests I saw that the problem is with deserialization of the JSON array to the map. I never get into the save method at the controller. Tried some annotation like @JsonDeserialize(as=HashMap.class) without success. How can I resolve this?

My POJO:

class MyClassId implements Serializable {
private static final long serialVersionUID = 5022985493208399875L;
String keyField1;
String keyField2;
}

@Entity
@IdClass(MyClassId.class)
public class MyClass {
@Id
String keyField1;

@Id
String keyField2;

String someField;

@ElementCollection
@MapKeyColumn(name="parameterName")
@Column(name="parameterValue", length=400)
Map<String, String> parameters;
... Getters and Setters ...

My controller:

@RestController
@RequestMapping("/myclass/**")
public class MyClassController {

@Transactional
@RequestMapping(value = "/save", method = RequestMethod.POST, consumes={"application/json"})
public @ResponseBody ServiceResponce<MyClass> save(@RequestBody MyClass processToSave) {
    ... Code ...
}
}
davide
  • 1,918
  • 2
  • 20
  • 30
Cony
  • 213
  • 1
  • 8
  • 21

2 Answers2

1

In the JSON, parameters is not an object but an array of objects:

"parameters":[{"parameterName":"dfgdfg","parameterValue":"sdf"}, ...

You can not map this on a

Map<String, String> parameters;

Use at least

List<Map<String, String>> parameters;
  • My map need to contain only "dfgdfg" and "sdf" without the "parameterName" and "parameterValue" which are const strings. – Cony Mar 02 '15 at 13:04
1

I see two solutions to your problem:

Hope it helps you, Thierry

Community
  • 1
  • 1
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360