java - 如何用 Java 编写这个用 C# 编写的逻辑? C#中的返回值类型有哪些?

标签 java c# spring

我有一个 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>

对吗?如何做到这一点?

最佳答案

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

关于java - 如何用 Java 编写这个用 C# 编写的逻辑? C#中的返回值类型有哪些?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62215410/

相关文章:

java - 为什么我无法将类导入到 Eclipse Maven 项目中的 java 文件中?

java - 在测试用例中覆盖 hibernate sessionfactory 的数据源

java - 如何在已编译的 Gradle 插件中运行命令行命令?

java - Android Future 与 FutureTask

c# - 遍历给定命名空间中的所有类并为每个类创建一个对象?

C#/.NET : Is `typeof(variable)` a possible language feature?

c# - ASP.NET MVC - 如何实现带有必填字段的可选嵌套模型?

java - 需要澄清引导 Spring Boot 应用程序的推荐方法

java - 下面这句话 : 是什么意思

Java 提示 Hadoop native 库