无法调用“String.hashCode()”,因为“<local4>”为空

问题描述 投票:0回答:1

执行测试时出现错误

package com.***.cmp.cgw.webclient.restcontroller.anagrafica;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;


import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@Disabled("Test per prove manuali, da raffinare")
@SpringBootTest
@AutoConfigureMockMvc
class RicercaAnagraficaBancaRestControllerTest {

    @Autowired
    private MockMvc mvc;

    @Test
    void getPersonaFisica() throws Exception {
        mvc.perform(MockMvcRequestBuilders
                        .get("/ricerca/anagrafica")
                        .param("abi", String.valueOf(55))
                        .param("ndg", "00259055")
                        .accept(MediaType.APPLICATION_JSON))
                .andDo(print())
                .andExpect(status().isOk());
    }
}

输出是

MockHttpServletResponse: 状态 = 500 错误消息 = 空 headers = [Vary:“来源”,“访问控制请求方法”,“访问控制请求标头”,内容类型:“application/json”, X-Content-Type-Options:"nosniff", X-XSS-Protection:"0", Cache-Control:"无缓存、无存储、max-age=0、必须重新验证", Pragma:“无缓存”,过期:“0”,X-Frame-Options:“DENY”] 内容类型=应用程序/json Body = {“errorMessage”:“无法调用“String.hashCode()”,因为“”为空”,“异常”:null} 转发的 URL = null 重定向的 URL = null 饼干=[]

被调用的控制器是

package com.****.cmp.cgw.webclient.restcontroller.anagrafica;

import com.****.cmp.cgw.webclient.dto.DatiAzienda;
import com.****.cmp.cgw.webclient.dto.DatiDittaIndividuale;
import com.****.cmp.cgw.webclient.dto.DatiPersonaFisica;
import com.****.cmp.cgw.webclient.dto.ricercabanca.ClienteDTO;
import com.****.cmp.cgw.webclient.dto.ricercabanca.Dati;
import com.****.cmp.cgw.webclient.dto.ricercabanca.Residenza;
import com.****.cmp.cgw.webclient.enumeration.TipoCliente;
import org.jetbrains.annotations.NotNull;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.****.cgw.webclient.model.integration.Ana02Repr;
import com.****.cgw.webclient.model.integration.Ana02Req;
import com.****.cmp.cgw.webclient.client.****IntegrationApiClient;

import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

import java.util.*;

import static com.****.cmp.cgw.webclient.enumeration.TipoCliente.*;

@RestController
@RequestMapping("/ricerca")
public class RicercaAnagraficaBancaRestController {

    @Autowired
    private ****IntegrationApiClient client;

    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "Cliente recuperato",
                    content = { @Content(mediaType = "application/json") }),})
    @GetMapping(path = "/anagrafica", produces = MediaType.APPLICATION_JSON_VALUE)
    public com.****.cmp.cgw.webclient.dto.ApiResponse<ClienteDTO>
    getClientByAnagrafica(@RequestParam int abi,
                          @RequestParam String ndg) {

        ResponseEntity<Ana02Repr> ana02ReprResponseEntity = client.ana02(createRequest(abi, ndg));

        //todo: cambiare setting una volta capito parametro

        return toApiResponse(ana02ReprResponseEntity);
    }

    private @NotNull Ana02Req createRequest(int abi, String ndg) {
        Ana02Req ana02Req = new Ana02Req();
        ana02Req.setCag(ndg);
        //todo: ndg da paddare a 8 con 0 iniziali
        ana02Req.setCodAbiUser(abi);
        ana02Req.codUser("fk0232"); //todo: utente di Ilario, da cambiare
        ana02Req.setCodFiliale("CE");
        return ana02Req;
    }

    private @NotNull com.****.cmp.cgw.webclient.dto.ApiResponse<ClienteDTO> toApiResponse(ResponseEntity<Ana02Repr> ana02ReprResponseEntity) {
        ClienteDTO clienteDTO = RicercaAnagraficaBancaMapper.INSTANCE.mapToClienteDTO(Objects.requireNonNull(ana02ReprResponseEntity.getBody()));

        com.****.cmp.cgw.webclient.dto.ApiResponse<ClienteDTO> apiResponse =
                new com.****.cmp.cgw.webclient.dto.ApiResponse<>();

        apiResponse.getContent().setValue(clienteDTO);

        apiResponse.setStatusCode(200);
        apiResponse.setMessage("Dettagli ndg recuperati correttamente");

        com.****.cmp.cgw.webclient.dto.ApiResponse.ApiAction apiAction =
                new com.****.cmp.cgw.webclient.dto.ApiResponse.ApiAction();

        apiAction.setMethod("GET");
        apiAction.setUrl("v1/searchReports");

        Map<String, com.****.cmp.cgw.webclient.dto.ApiResponse.ApiAction> action = new HashMap<>();

        action.put("search", apiAction);

        apiResponse.getContent().setActions(action);
        return apiResponse;
    }

    @Mapper
    @Component
    public interface RicercaAnagraficaBancaMapper {
        RicercaAnagraficaBancaMapper INSTANCE = Mappers.getMapper(RicercaAnagraficaBancaMapper.class);

        //todo: refactoring con funzioni mapstruct


        default ClienteDTO mapToClienteDTO(Ana02Repr ana02Repr){
            String segmentoCommerciale = ana02Repr.getSegmCommerciale();

            //todo: mapping da confermare
            TipoCliente tipoCliente = switch (segmentoCommerciale) {
                case "PF", "DF" -> PERSONA_FISICA;
                case "DI" -> DITTA_INDIVIDUALE;
                case "CM", "CG", "CF", "SG", "NG", "PG" -> SOCIETA;
                default -> throw new IllegalStateException("Valore non previsto in ana02 " +
                        "per segmento commerciale: " + segmentoCommerciale);
            };

            Residenza residenza = switch (tipoCliente) {
                case PERSONA_FISICA -> toResidenzaPersonaFisica(ana02Repr);
                case DITTA_INDIVIDUALE, SOCIETA -> toResidenzaPersonaGiuridica(ana02Repr);
            };

            Dati dati = switch (tipoCliente) {
                case PERSONA_FISICA -> toDatiPersonaFisica(ana02Repr);
                case DITTA_INDIVIDUALE -> toDatiDittaIndividuale(ana02Repr);
                case SOCIETA -> toDatiSocieta(ana02Repr);
            };

            ClienteDTO clienteDTO = new ClienteDTO();

            clienteDTO.setTipologia(tipoCliente.toString());
            clienteDTO.setResidenza(residenza);
            clienteDTO.setDati(dati);

            return clienteDTO;
        }

        @Mapping(source = "resIndirizzo", target = "indirizzo")
        @Mapping(source = "resComune", target = "localita")
        @Mapping(source = "resCap", target = "cap")
        @Mapping(source = "resProvincia", target = "provincia")
        @Mapping(source = "resNazDes", target = "nazione")
        Residenza toResidenzaPersonaFisica(Ana02Repr source);

        @Mapping(source = "legIndirizzo", target = "indirizzo")
        @Mapping(source = "legComune", target = "localita")
        @Mapping(source = "legCap", target = "cap")
        @Mapping(source = "legProvincia", target = "provincia")
        @Mapping(source = "legNazDes", target = "nazione")
        Residenza toResidenzaPersonaGiuridica(Ana02Repr source);

        //todo: la data come va formattata?
        @Mapping(source = "codFiscale", target = "codiceFiscale")
        @Mapping(source = "sesso", target = "sesso")
        @Mapping(source = "nome", target = "nome")
        @Mapping(source = "cognome", target = "cognome")
        @Mapping(source = "nascData", target = "dataNascita")
        @Mapping(source = "nascLuogo", target = "luogoNascita")
        @Mapping(source = "nascProvincia", target = "provinciaNascita")
        @Mapping(source = "nascNazDes", target = "nazioneNascita")
        DatiPersonaFisica toDatiPersonaFisica(Ana02Repr source);

        @Mapping(source = "intestaz", target = "denominazione")
        @Mapping(source = "codFiscale", target = "codiceFiscale")
        @Mapping(source = "sesso", target = "sesso")
        @Mapping(source = "nome", target = "nome")
        @Mapping(source = "cognome", target = "cognome")
        @Mapping(source = "nascData", target = "dataNascita")
        @Mapping(source = "nascLuogo", target = "luogoNascita")
        @Mapping(source = "nascProvincia", target = "provinciaNascita")
        @Mapping(source = "nascNazDes", target = "nazioneNascita")
        @Mapping(source = "raeCod", target = "numeroRea")
        @Mapping(source = "cciaaProvincia", target = "provinciaCCIA")
        @Mapping(source = "partitaIva", target = "partitaIVA")
        DatiDittaIndividuale toDatiDittaIndividuale(Ana02Repr source);

        @Mapping(source = "intestaz", target = "denominazione")
        @Mapping(source = "desTipoControparte", target = "specieGiuridica")
        @Mapping(source = "raeCod", target = "numeroRea")
        @Mapping(source = "cciaaProvincia", target = "provinciaCCIA")
        @Mapping(source = "partitaIva", target = "partitaIVA")
        @Mapping(source = "codFiscale", target = "codiceFiscale")
        DatiAzienda toDatiSocieta(Ana02Repr source);

    }

}

问题是我找不到这个错误与什么相关。即使在网络上也只有一个例子,但似乎不是我的情况。

java spring spring-boot mapstruct spring-test-mvc
1个回答
0
投票

问题在于解析错误的字段。

ana02Repr.getSegmCommerciale()
始终返回空。我仍然不知道为什么它没有返回更具描述性的错误。

© www.soinside.com 2019 - 2024. All rights reserved.