英文:
How to write this logic, made in C#, in Java? What the types of the returns in C#?
问题
C#(重要的是方法 [Route("ClienteSemSaldo")]:)
[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):
@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 代码中的 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")]:
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 (the important is the method 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);
}
}
So the method getClienteSemSaldo must return
List<CustomerProductResponse>
And not
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?
专注分享java语言的经验与见解,让所有开发者获益!
评论