[如何用Java用C#编写此逻辑? C#中返回的类型是什么?

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

我有一个C#API,只需要将其移植到Spring API(Java),但是我在使用C#语言的某些逻辑时遇到了麻烦。这都是C#和Java中的代码。

C#(重要的是方法[Route(“ ClienteSemSaldo”)]:

    namespace Web.Api.Rest.Controllers
{
    [Route("api/[controller]")]
    [ApiExplorerSettings(IgnoreApi = false)]
    public class RcClienteController : Controller
    {
        private readonly IRcClienteService _service;
        private readonly IRcClienteProdutoService _serviceProduto;
        private string _userId;
        private string _userTipo;
        private static readonly ILog log = LogManager.GetLogger(typeof(RcClienteController));

        public RcClienteController(IRcClienteService service, IRcClienteProdutoService serviceProduto)
        {
            _service = service;
            _serviceProduto = serviceProduto;
            _userTipo = "E";
        }

        [HttpGet]
        [ProducesResponseType(typeof(RcCliente), 200)]
        public async Task<IActionResult> Get([FromQuery]string cliente)
        {
            try
            {
                _userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
                var t = await _service.BuscarCliente(_userId, _userTipo, cliente);
                return Ok(t);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return BadRequest(ex.Message);
            }
        }

        [HttpGet]
        [Route("ClienteSemSaldo")]
        [ProducesResponseType(typeof(RcCliente), 200)]
        public async Task<IActionResult> ClienteSemSaldo()
        {
            try
            {
                _userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;

                var posicoes = await _serviceProduto.BuscarClienteProduto(_userId, _userTipo, null, null);
                var clientes = await _service.BuscarCliente(_userId, _userTipo, null);

                var t = clientes.Where(a => !posicoes.Any(b => b.ClienteId == a.ClienteId)).ToList();
                return Ok(t);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return BadRequest(ex.Message);
            }
        }

    }
}

Java(重要的是方法getClienteSemSaldo):

@RequestMapping("/customer")
@RestController
public class CustomerController implements ICustomerController{

    private final String _userTipo = "E";

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ICustomerProductService customerProductService;


    @GetMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<CustomerResponse>> getCliente(@RequestParam String cliente) throws Exception {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String idUser = authentication.getName();

        List<CustomerResponse> lista = customerService.getCliente(cliente, idUser, _userTipo);
        return new ResponseEntity<List<CustomerResponse>>(lista, HttpStatus.OK);
    }

    @GetMapping(path="/withoutbalance", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<CustomerProductResponse>> getClienteSemSaldo() throws Exception {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String idUser = authentication.getName();

        List<CustomerProductResponse> posicoes = customerProductService.getClienteProduto(null, idUser, _userTipo, null);
        List<CustomerResponse> clientes = customerService.getCliente(null, idUser, _userTipo);

        List<CustomerProductResponse> lista = posicoes.stream()
                                        .filter(a -> !posicoes.stream().anyMatch(b -> b.clienteId == a.clienteId))
                                        //.map(b -> new CustomerProductResponse())
                                        .collect(Collectors.toList());

        return new ResponseEntity<List<CustomerProductResponse>>(lista, HttpStatus.OK);
    }

}

因此方法getClienteSemSaldo必须返回

List<CustomerProductResponse>

不是

List<CustomerResponse>

对吗?怎么做?

java c# spring
1个回答
0
投票

我已经分析了您的代码,有些困惑。您的方法getClienteSemSaldo()返回ResponseEntity>的类型,这就是您在方法末尾返回的类型。您可以使用具有通用参数类型的ResponseEntity类构造函数,并传递从已过滤的posicoes列表创建的“ lista”列表。这意味着它们是相同的类型(列表)。我错过了什么吗?

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