如何在Feign中正确使用带有原始类型的ResponseEntity?

时间:2019-10-25 14:32:33

标签: java spring feign

我有一个使用Feign的客户端服务器应用程序。服务器部分(没有业务逻辑)是:

@RestController
@RequestMapping
public class ControllerAddress {

    @PostMapping(value = "/address")
    @ResponseBody
    public ResponseEntity<Boolean> postAddress(@RequestBody String address) {
        return ResponseEntity.ok(true);
    }
}

客户端部分具有服务器界面,如下所示:

public interface ServiceInterfaceAddress {

    @Path("/address")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    ResponseEntity<Boolean> setAddress(@RequestBody String address);
}

然后我继续通过以下方式创建并调用Feign服务

public class CallerServiceInterfaceAddress {

    public static ResponseEntity<Boolean> call(String url, String address) {
        ServiceInterfaceAddress serviceInterfaceAddress = Feign.builder()
            .contract(new JAXRSContract())
            .encoder(new JacksonEncoder())
            .decoder(new JacksonDecoder())
            .target(ServiceInterfaceAddress.class, url);
        return serviceInterfaceAddress.setAddress(address);
    }
}

我得到的是     com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.springframework.http.ResponseEntity: no boolean/Boolean-argument constructor/factory method to deserialize from boolean value (true)服务器收到响应时。

如果我将我的客户部分中两个函数的返回类型从ResponseEntity<Boolean>更改为boolean,它将起作用。我应该保持这种方式还是有可能不用太多麻烦就能修复原始代码并保留ResponseEntity<Boolean>返回类型?

0 个答案:

没有答案