I want to create a function that receives a JSON object and map it to the right class structure. I am using Postman to send the JSON object rest API { "id" :1 }
I have been looking for a solution to fix the upper exception but none of the suggested answers solve my problem. By the way I imported " EclipseLink " library and that did not help.
package com.books.controller;
import java.io.Serializable;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;
@RestController
@RequestMapping("api/v1/")
public class StrongBadController{
@RequestMapping(value="strongbad/objectKey",method = RequestMethod.POST, consumes="application/json")
public String unMarshallObject(@RequestBody String jsonObject) throws JAXBException{
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(UnmarshallerProperties.MEDIA_TYPE, "application/json");
JAXBContext jc = JAXBContext.newInstance(new Class[] {StrongBad.class}, properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StringReader json =new StringReader(jsonObject);
StrongBad strongBad2 = (StrongBad) unmarshaller.unmarshal(json);
return "";
}
class StrongBad implements Serializable{
private static final long serialVersionUID = 1L;
private String id;
public StrongBad() {
super();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
}
Aucun commentaire:
Enregistrer un commentaire