簡體   English   中英

對象映射器給出異常:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:無法識別的字段

[英]Object Mapper giving Exception: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field

我正在使用 Spring Boot 應用程序,在使用來自 Postman 的 GET 請求點擊“/test/api”rest 端點時,出現以下錯誤:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:無法識別的字段“userName”(com.example.MyPojo 類),未標記為可忽略(0 個已知屬性:])

我嘗試使用的服務以以下格式生成響應。

@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;
}

我的 Controller 類如下所示。

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;
}

同樣,對於 Product,您需要 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM