怎样将这段用C#编写的逻辑翻译成Java?C#中的返回类型是什么?

huangapple 未分类评论53阅读模式
英文:

How to write this logic, made in C#, in Java? What the types of the returns in C#?

问题

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

  1. [HttpGet]
  2. [Route("ClienteSemSaldo")]
  3. [ProducesResponseType(typeof(RcCliente), 200)]
  4. public async Task<IActionResult> ClienteSemSaldo()
  5. {
  6. try
  7. {
  8. _userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
  9. var posicoes = await _serviceProduto.BuscarClienteProduto(_userId, _userTipo, null, null);
  10. var clientes = await _service.BuscarCliente(_userId, _userTipo, null);
  11. var t = clientes.Where(a => !posicoes.Any(b => b.ClienteId == a.ClienteId)).ToList();
  12. return Ok(t);
  13. }
  14. catch (Exception ex)
  15. {
  16. log.Error(ex);
  17. return BadRequest(ex.Message);
  18. }
  19. }

Java(重要的是方法 getClienteSemSaldo):

  1. @GetMapping(path="/withoutbalance", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  2. public ResponseEntity<List<CustomerProductResponse>> getClienteSemSaldo() throws Exception {
  3. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  4. String idUser = authentication.getName();
  5. List<CustomerProductResponse> posicoes = customerProductService.getClienteProduto(null, idUser, _userTipo, null);
  6. List<CustomerResponse> clientes = customerService.getCliente(null, idUser, _userTipo);
  7. List<CustomerProductResponse> lista = posicoes.stream()
  8. .filter(a -> !posicoes.stream().anyMatch(b -> b.clienteId == a.clienteId))
  9. //.map(b -> new CustomerProductResponse())
  10. .collect(Collectors.toList());
  11. return new ResponseEntity<List<CustomerProductResponse>>(lista, HttpStatus.OK);
  12. }

所以,方法 getClienteSemSaldo 必须返回 List<CustomerProductResponse>,而不是 List<CustomerResponse>。是的,你是正确的。如何实现呢?

在 Java 代码中的 getClienteSemSaldo 方法中,你已经创建了一个名为 lista 的列表,其中包含过滤后的 CustomerProductResponse 对象。这是正确的。只需确保在函数签名和返回类型中指定正确的类型,就能达到预期的结果。

英文:

I have an C# API and just need to port it to Spring API (Java), but I'm having trouble with some logics of C# language.
Here's both codes in C# and Java.

C# (the important is the method [Route("ClienteSemSaldo")]:

  1. namespace Web.Api.Rest.Controllers
  2. {
  3. [Route("api/[controller]")]
  4. [ApiExplorerSettings(IgnoreApi = false)]
  5. public class RcClienteController : Controller
  6. {
  7. private readonly IRcClienteService _service;
  8. private readonly IRcClienteProdutoService _serviceProduto;
  9. private string _userId;
  10. private string _userTipo;
  11. private static readonly ILog log = LogManager.GetLogger(typeof(RcClienteController));
  12. public RcClienteController(IRcClienteService service, IRcClienteProdutoService serviceProduto)
  13. {
  14. _service = service;
  15. _serviceProduto = serviceProduto;
  16. _userTipo = "E";
  17. }
  18. [HttpGet]
  19. [ProducesResponseType(typeof(RcCliente), 200)]
  20. public async Task<IActionResult> Get([FromQuery]string cliente)
  21. {
  22. try
  23. {
  24. _userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
  25. var t = await _service.BuscarCliente(_userId, _userTipo, cliente);
  26. return Ok(t);
  27. }
  28. catch (Exception ex)
  29. {
  30. log.Error(ex);
  31. return BadRequest(ex.Message);
  32. }
  33. }
  34. [HttpGet]
  35. [Route("ClienteSemSaldo")]
  36. [ProducesResponseType(typeof(RcCliente), 200)]
  37. public async Task<IActionResult> ClienteSemSaldo()
  38. {
  39. try
  40. {
  41. _userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
  42. var posicoes = await _serviceProduto.BuscarClienteProduto(_userId, _userTipo, null, null);
  43. var clientes = await _service.BuscarCliente(_userId, _userTipo, null);
  44. var t = clientes.Where(a => !posicoes.Any(b => b.ClienteId == a.ClienteId)).ToList();
  45. return Ok(t);
  46. }
  47. catch (Exception ex)
  48. {
  49. log.Error(ex);
  50. return BadRequest(ex.Message);
  51. }
  52. }
  53. }
  54. }

Java (the important is the method getClienteSemSaldo):

  1. @RequestMapping("/customer")
  2. @RestController
  3. public class CustomerController implements ICustomerController{
  4. private final String _userTipo = "E";
  5. @Autowired
  6. private ICustomerService customerService;
  7. @Autowired
  8. private ICustomerProductService customerProductService;
  9. @GetMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  10. public ResponseEntity<List<CustomerResponse>> getCliente(@RequestParam String cliente) throws Exception {
  11. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  12. String idUser = authentication.getName();
  13. List<CustomerResponse> lista = customerService.getCliente(cliente, idUser, _userTipo);
  14. return new ResponseEntity<List<CustomerResponse>>(lista, HttpStatus.OK);
  15. }
  16. @GetMapping(path="/withoutbalance", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  17. public ResponseEntity<List<CustomerProductResponse>> getClienteSemSaldo() throws Exception {
  18. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  19. String idUser = authentication.getName();
  20. List<CustomerProductResponse> posicoes = customerProductService.getClienteProduto(null, idUser, _userTipo, null);
  21. List<CustomerResponse> clientes = customerService.getCliente(null, idUser, _userTipo);
  22. List<CustomerProductResponse> lista = posicoes.stream()
  23. .filter(a -> !posicoes.stream().anyMatch(b -> b.clienteId == a.clienteId))
  24. //.map(b -> new CustomerProductResponse())
  25. .collect(Collectors.toList());
  26. return new ResponseEntity<List<CustomerProductResponse>>(lista, HttpStatus.OK);
  27. }
  28. }

So the method getClienteSemSaldo must return

  1. List<CustomerProductResponse>

And not

  1. List<CustomerResponse>

Right? How to do that?

答案1

得分: 0

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

英文:

I have analysed your code and I am a little bit confused. Your method getClienteSemSaldo() returns type of ResponseEntity<List<CustomerProductResponse>> and that is the type you return at the end of the method. You use ResponseEntity class constructor with generic argument type and pass "lista" list which was created from filtered posicoes list. That means, they are the same type (List< CustomerProductResponse >). Did I miss something?

huangapple
  • 本文由 发表于 2020年6月5日 20:30:08
  • 转载请务必保留本文链接:https://java.coder-hub.com/62215410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定