TestUser.java
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.annotate.JsonProperty;
@XmlRootElement
public class TestUser {
/*@XmlElement(name="username")*/
@JsonProperty("username")
private String userName;
private String email;
public String getEmail() {
return email;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "TestUser [userName=" + userName + ", email=" + email + "]";
}
}
Test.java
import java.io.IOException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Test {
private Logger logger = LoggerFactory.getLogger(Test.class);
/*private String getBaseUri()
{
return "http://192.168.1.223:8080/restfull-api";
}*/
@Test
public void testTest()
{
ObjectMapper mapper = new ObjectMapper();
/*AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
// make deserializer use JAXB annotations (only)
mapper.getDeserializationConfig().setAnnotationIntrospector(introspector);
// make serializer use JAXB annotations (only)
mapper.getSerializationConfig().setAnnotationIntrospector(introspector);*/
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String userStr = "{\"username\":\"paul\",\"email\":\"paul@paul.com\"}";
try {
TypeReference<TestUser> temp = new TypeReference<TestUser>(){};
TestUser testUser = mapper.readValue(userStr, temp);
System.out.println(testUser.toString());
String result = mapper.writeValueAsString(testUser);
System.out.println(result);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}