对象映射器给出异常:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段
Object Mapper giving Exception: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field
我正在使用 spring 引导应用程序,在使用来自 Postman 的 GET 请求点击“/test/api”休息端点时,出现以下错误:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "userName" (class
com.example.MyPojo), not marked as ignorable
(0 known properties: ])
我尝试使用的服务生成以下格式的响应。
@Getter
@Setter
public class MyResponse extends MyPojo {
int responseCode;
String responseMessage;
List<MyPojo> output;
}
public class MyPojo{
}
public class User extends MyPojo {
private String id;
@NotBlank
private String userName;
private String companyId;
}
我的控制器 class 看起来像下面的东西。
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
@RestController
@RequestMapping("/test")
public class SampleRestController {
@GetMapping("/api")
public MyResponse testApi(){
RestTemplate restTemplate = new RestTemplate();
String url="http://<Domain>:8085/users/active";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>("header",headers);
final ResponseEntity<String> responseEntity = restTemplate.exchange( url, HttpMethod.GET, entity, String.class );
MyResponse myResponse = null;
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility( PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.enable( DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
try {
myResponse = mapper.readValue(responseEntity.getBody(), MyResponse.class);
} catch (IOException e) {
e.printStackTrace();
}
return myResponse;
}
}
请指出我的错误,我想不通。任何帮助将不胜感激。
要映射用户属性,您需要有 UserResponse-
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserResponse extends MyPojo {
int responseCode;
String responseMessage;
List<User> output;
}
同样,对于产品,您需要 ProductResponse-
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProductResponse extends MyPojo {
int responseCode;
String responseMessage;
List<Product> output;
}
此外,如果当前未使用,请定义@Getter 和@Setter 注释。
@Getter
@Setter
public class User extends MyPojo {
private String id;
@NotBlank
private String userName;
private String companyId;
}
我正在使用 spring 引导应用程序,在使用来自 Postman 的 GET 请求点击“/test/api”休息端点时,出现以下错误:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "userName" (class com.example.MyPojo), not marked as ignorable (0 known properties: ])
我尝试使用的服务生成以下格式的响应。
@Getter
@Setter
public class MyResponse extends MyPojo {
int responseCode;
String responseMessage;
List<MyPojo> output;
}
public class MyPojo{
}
public class User extends MyPojo {
private String id;
@NotBlank
private String userName;
private String companyId;
}
我的控制器 class 看起来像下面的东西。
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
@RestController
@RequestMapping("/test")
public class SampleRestController {
@GetMapping("/api")
public MyResponse testApi(){
RestTemplate restTemplate = new RestTemplate();
String url="http://<Domain>:8085/users/active";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>("header",headers);
final ResponseEntity<String> responseEntity = restTemplate.exchange( url, HttpMethod.GET, entity, String.class );
MyResponse myResponse = null;
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility( PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.enable( DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
try {
myResponse = mapper.readValue(responseEntity.getBody(), MyResponse.class);
} catch (IOException e) {
e.printStackTrace();
}
return myResponse;
}
}
请指出我的错误,我想不通。任何帮助将不胜感激。
要映射用户属性,您需要有 UserResponse-
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserResponse extends MyPojo {
int responseCode;
String responseMessage;
List<User> output;
}
同样,对于产品,您需要 ProductResponse-
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProductResponse extends MyPojo {
int responseCode;
String responseMessage;
List<Product> output;
}
此外,如果当前未使用,请定义@Getter 和@Setter 注释。
@Getter
@Setter
public class User extends MyPojo {
private String id;
@NotBlank
private String userName;
private String companyId;
}