Spring 6升级问题|JSON响应具有额外的“ArrayList”和“$type”

wfauudbj  于 2023-04-20  发布在  Spring
关注(0)|答案(1)|浏览(122)

当使用旧版本的spring [4]时,下面是JSON响应:

{
    "processStatus": "0",
    "processStatusDescription": "",
    "processErrorMessage": "",
    "registrationLocator": [
            {                
                "RegistrarCode": "1042",
                "RegistrarLocCode": "KA008",
                "RegistrarPhoneNumber": "1-866-771-0140"                
            }        
    ],
    "processLoc": "KA",
    "processAlternatives": true
}

升级到Spring版本6后,下面是JSON响应

{
    "processStatus": "0",
    "processStatusDescription": "",
    "processErrorMessage": "",
    "registrationLocator": [
        "ArrayList",
        [
            {
                "$type": "LinkedHashMap",
                "RegistrarCode": "1042",
                "RegistrarLocCode": "KA008",
                "RegistrarPhoneNumber": "1-866-771-0140"                
            }
        ]
    ],
    "processLoc": "KA",
    "processAlternatives": true
}

您可以注意到现在出现了额外的“ArrayList”和“$type”属性。
有人能帮忙让JSON响应和旧的一样吗[Spring 4]?
下面是控制器方法:

@GetMapping(value = {REGISTRAR_LOCATOR})
public @ResponseBody Map<String, Object> registrarLocator(RegistrarLocatorVO requestVO, HttpServletRequest request, HttpServletResponse response) {
    
    try {   
        Map<String, Object> responseMap = registrarLocatorUtiliy.getRegistrar(requestVO, webRequestContext, request);
                            
        return responseMap;
    } catch (Exception ex) {
        log.error("Exception in registrarLocator => ", ex);
    }
}
6rqinv9w

6rqinv9w1#

Jackson中的升级导致了这一点,因为它现在也Map了Object的类型。你真的应该创建一个POJO来正确Map它。

public class Registrar {
 private String RegistrarCode;
 private String RegistrarLocCode;
 private String RegistrarPhoneNumber;

 // Getters and Setters
}
public class RegistrarResponse {
    private String processStatus;
    private String processStatusDescription;
    private String processErrorMessage;
    private String processLoc;
    private boolean processAlternatives;

    private List<Registrar> registrationLocator;
    
    // Getters and Setters
}

然后将代码更新为

@GetMapping(value = {REGISTRAR_LOCATOR})
public @ResponseBody RegistrarResponse registrarLocator(RegistrarLocatorVO requestVO, HttpServletRequest request, HttpServletResponse response) {
    
    try {   
        RegistrarResponse response = registrarLocatorUtiliy.getRegistrar(requestVO, webRequestContext, request);
                            
        return response;
    } catch (Exception ex) {
        log.error("Exception in registrarLocator => ", ex);
    }
}

当然,类名只是建议。

相关问题