【问题标题】:How to validate the response of my java service in angular?如何以角度验证我的 java 服务的响应?
【发布时间】:2021-10-10 18:10:05
【问题描述】:

我有一个 Java 服务,它查询数据库并返回一个数据列表,我必须在前面的表格中用 angular 显示这些数据,所以我做了这个方法,它返回一个哈希映射来了解查询时是否发生错误或知道日期范围内没有数据,因此它返回一个错误代码,因为我想在前面验证此代码并在我前面显示一条消息,即除了显示

Java 控制器

@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/consultValues")
public class ValuesController {
    
    @GetMapping
    public Map<String, Object>  consultValues(
            @RequestParam(required = false, value = "startDate") Integer startDate,
            @RequestParam(required = false, value = "endDate") Integer endDate) throws Exception {
        Map<String, Object>  listValues = new HashMap<>();
        try {
            listValues = valuesService.listValues(startDate, endDate);
        } catch (Exception e) {
            LOGGER.error("Error in Values ");
            throw e;
        }
        return listValues;
    }

}

Java 服务

@Override
public Map<String, Object> listValues(Integer startDate, Integer endDate) throws Exception {
    Map<String, Object> response = new HashMap<>();
    List<ValuesDto> list = new ArrayList<ValuesDto>();
    try {
        Integer start = startDate;
        Integer end = endDate;
        list = valuesRepository.findByDates(start, end);
        if (list.isEmpty() || list == null) {
            LOGGER.error("There is not values");
            response.put("Error", 400);
        }

    } catch (Exception e) {
        LOGGER.error("An error ocurred");
        response.put("Error", 500);
        response.put("error", e.getMessage());
        throw e;
    }
    response.put("success", true);
    response.put("data", list);
    return response;

}

现在在我的前面我有我的 service.ts 的这个方法

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {ValoresDto} from '../Models/ValoresDTO';

@Injectable({
    providedIn: 'root'
})
export class ConsultValuesService {

    constructor(private http: HttpClient) { }
    Url = 'http://localhost:8080/consultValues';


    consultValues(startDate:any, endDate:any){
        return this.http.get<ValuesDTO[]>(this.Url+ `?startDate=${startDate}&endDate=${endDate}`);
    }
}

在我的 Component.ts 中,我有这个方法,但我不知道如何验证我的 java 服务的响应,例如,如果我的 java 服务返回代码错误 400 表示没有数据并显示一条带有 Swal 的消息,如果返回 500 发生错误并使用 Swal 显示消息,或者成功返回列表并填写我的表格

getValuesDB() {
    if (this.valuesForm.valid) {
        this.service.consultValues(this.endDate, this.endDate).subscribe({
        next: data => {
        this.modelConsultValues=data;
        },
         error: (err) => console.error(err),
         complete: () => console.log('Completed')
        });
  

    } else {
        Swal.fire({
        icon: 'error',
        text: 'You must select both dates'
        })
    }
}

请任何人帮助我,我如何验证我的 java 服务的响应并显示表格填充或消息

【问题讨论】:

    标签: java angular spring-boot


    【解决方案1】:

    这是自以为是的,但无论如何我都会发布一个链接。 class-validator 让我们用装饰器为模型增添趣味,然后验证实例。

    https://github.com/typestack/class-validator

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 2020-06-24
      • 2012-02-15
      • 2014-09-27
      • 1970-01-01
      相关资源
      最近更新 更多