У меня есть подобный класс Бина, как в моем коде. Я использую java7 и Gradle 4.10.3. Используя JsonIgnore, я не смог игнорировать поле catColor, но используя @XmlTransient, я смог игнорировать поле. Может кто-нибудь сообщить мне, почему это происходит, и тестовый пример для тестирования в полевых условиях? Я попробовал тестовый класс junit, как показано ниже, но я получаю исключение нулевого указателя в строке, отмеченной ***.
CatBean.java
import com.fasterxml.jackson.annotation.JsonIgnore;
@XmlRootElement
@SuppressWarnings("PMD")
public class CatBean {
private String catName;
private String catEyes;
private String catDetails;
public String getCatName(){
return catName;
}
public void setCatName(String catName){
this.catName=catName;
}
public String getCatEyes(){
return catEyes;
}
public void setCatEyes(String catEyes){
this.catEyes=catEyes;
}
public String getCatDetails(){
return catDetails;
}
public void setCatDetails(String catDetails){
this.catDetails=catDetails;
}
@JsonIgnore
public String getCatColor() {
return getCatDetails("Cat Color");
}
public void setCatColor(){
setCatDetails("Cat Color", value);
}
public String getCatDetails(String field){
if(field=="Cat Color"){
return blue;
}
}
public void setCatDetails(String field, String value){
CatDetails catDetails=new CatDetails();
catDetails.setName(field);
catDetails.setValue(value);
}
}
CatDetails.java
public class CatDetails {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Junit Test Case for @JsonIgnore
@Test
public void testJsonResponseNotContainingCatColor() {
CatBean catBean= new CatBean();
ObjectMapper mapper = new ObjectMapper();
String catBeanAsString = Mapper.writeValueAsString(catBean);
assertThat(requestBeanAsString,not(containsString("catColor")));
}
Junit Test Case for @XmlTransient
@Test
public void testJsonResponseAfterXmlTransient()
{
CatBean catBean = new CatBean();
JSONHelper jsonHelper = new JSONHelper();
assertNotNull(requestBean);
*** String requestBeanAsString = jsonHelper.marshall(requestBean);
// assertThat(requestBeanAsString,not(containsString("cardPresent")));
assertEquals(requestBean.getCardPresent(),"1");
}
JSONHelper.java
public class JSONHelper {
private JAXBContextResolver jaxbContextResolver;
public String marshall(Object bean) {
try {
JAXBContext context = jaxbContextResolver.getContext(bean.getClass());
if (context == null) {
context = JAXBContext.newInstance(bean.getClass());
}
ByteArrayOutputStream marshalledStream = new ByteArrayOutputStream();
JSONJAXBContext.getJSONMarshaller(context.createMarshaller(), context).marshallToJSON(bean,
marshalledStream);
return marshalledStream.toString();
} catch (JAXBException e) {
throw new HostAuthException("Unable to marshall bean: " + e.getMessage(), e);
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public Object unmarshall(String bs, Class c) {
try {
JAXBContext context = jaxbContextResolver.getContext(c);
JSONUnmarshaller jsonUnmarshaller = JSONJAXBContext.getJSONUnmarshaller(context.createUnmarshaller(),
context);
return jsonUnmarshaller.unmarshalFromJSON(new ByteArrayInputStream(bs.getBytes()), c);
} catch (JAXBException e) {
throw new HostAuthException("Unable to unmarshall bean: " + e.getMessage(), e);
}
}
public void setJaxbContextResolver(JAXBContextResolver jaxbContextResolver) {
this.jaxbContextResolver = jaxbContextResolver;
}
}