【问题标题】:How to send a java object to a get method trought angular JS如何通过angularJS将java对象发送到get方法
【发布时间】:2019-10-18 18:11:16
【问题描述】:

我必须在 Java 服务器中进行搜索,并且需要将参数作为对象传递。我使用 HttpParams 将搜索字段作为字符串传递。但现在我需要将它作为具有 9 个字符串的对象传递,因为声纳只接受 utinl 7 个参数。我需要继续使用 get 方法。我该怎么做?

这是服务器中get方法的签名:

@GetMapping("/buscarPorFiltros")
public ResponseEntity<ProjetoResponse> buscarPorFiltros(PesquisaDTO pesquisa){
}

这里是 PesquisaDTO 类:

public class PesquisaDTO {

    private Optional<String> dsProjeto;
    private Optional<String> nomeProjeto;
    private Optional<Date> dataInicio;
    private Optional<Date> dataFim;
    private Optional<String> statusProjeto;
    private Optional<String> un;
    private Optional<String> setor;
    private Optional<String> gerenteProjetoNome;
    private Optional<String> serviceType;

   public PesquisaDTO() {
   }

   public String getDsProjeto() {
       return dsProjeto.isPresent() ? dsProjeto.get() : "";
   }

   // others getters similar to getDsProject()

这里是角度函数:

 public buscarPorFiltros(dsProjeto, nomeProjeto, statusProjeto, dtInicio, dtFim, un, setor, gerenteProjetoNome, serviceType): Observable<ProjetoResponse> {

      varpesquisa: Pesquisa;

      if (dsProjeto) {
          pesquisa.dsProjeto = dsProjeto;
      }
      if (nomeProjeto) {
          pesquisa.nomeProjeto = nomeProjeto;
      }
      if (statusProjeto) {
          pesquisa.statusProjeto = statusProjeto;
      }
      if (dtInicio && dtFim) {
          pesquisa.dataInicio = dtInicio;
          pesquisa.dataFim = dtFim;
      }
      if (un) {
         pesquisa.un = un;
      }
      if (setor) {
         pesquisa.setor = setor;
     }

     if (gerenteProjetoNome) {
        pesquisa.gerenteProjetoNome = gerenteProjetoNome;
     }

     if (serviceType){
        pesquisa.serviceType = serviceType;
     }

      return this.httpClient.get<ProjetoResponse>(this.applicationUrl + mappingUrls.projetoService.buscarPorFiltros, { pesquisa });

 }

Angular 不接受“pesquisa”作为参数 它说:

Argument of type '{ pesquisa: Pesquisa; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.   Object literal may only specify known properties, and 'pesquisa' does not exist in type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.

有人可以帮助我吗?

【问题讨论】:

  • 当然,GET 我们不发送正文。你的意思是使用POST
  • 对不起,我必须使用 get 方法,因为它是一个搜索。我编辑了我的问题。我想现在更清楚了。
  • 那么您可能希望将它们设置为 HttpParams: angular.io/guide/http#configuring-the-request
  • 如果你想发送一个大的dto作为请求体,你必须使用post。获取标准不支持这一点。要么使用httpParams,要么改成post。为什么你必须使用get?
  • 我可以使用 JSON.stringify 转换字符串中的角度对象,然后在服务器中接收这个字符串作为我的 get 方法的参数吗?

标签: java angular spring-boot


【解决方案1】:

在服务器端:

@GetMapping("/buscarPorFiltros")
@Consumes(MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ProjetoResponse> buscarPorFiltros(String pesquisa ){
    try {
        String result = java.net.URLDecoder.decode(pesquisa, StandardCharsets.UTF_8.name());
        ObjectMapper mapper = new ObjectMapper();
        PesquisaDTO pesquisaDto = mapper.readValue(result, PesquisaDTO.class);

在客户端:

  const params = new HttpParams().set("pesquisa", encodeURIComponent(JSON.stringify(pesquisa)));
  return this.httpClient.get<ProjetoResponse>(this.applicationUrl + mappingUrls.projetoService.buscarPorFiltros, {params});

【讨论】:

    【解决方案2】:

    你需要使用 HttpParams Agular 类来构建你的 url 字符串,请求参数如下:

     public buscarPorFiltros(dsProjeto, nomeProjeto, statusProjeto, dtInicio, dtFim, un, setor, gerenteProjetoNome, serviceType): Observable<ProjetoResponse> {
      const requestParams = new HttpParams();
    
      if (dsProjeto) {
          requestParams.append('dsProjeto', dsProjeto);
      }
      if (nomeProjeto) {
          requestParams.append('nomeProjeto', nomeProjeto);
      }
      if (statusProjeto) {
          requestParams.append('statusProjeto', statusProjeto);
      }
      if (dtInicio && dtFim) {
          requestParams.append('dtInicio', dtInicio);
          requestParams.append('dtFim', dtFim);
      }
      if (un) {
         requestParams.append('un', un);
      }
      if (setor) {
         requestParams.append('setor', setor);
     }
    
     if (gerenteProjetoNome) {
         requestParams.append('gerenteProjetoNome', gerenteProjetoNome);
     }
    
     if (serviceType){
         requestParams.append('serviceType', serviceType);
     }
    
      return this.httpClient.get<ProjetoResponse>(this.applicationUrl + mappingUrls.projetoService.buscarPorFiltros, requestParams);
    }
    

    在服务器端,您需要像现在这样拥有控制器:

    @GetMapping("/buscarPorFiltros")
    public ResponseEntity<ProjetoResponse> buscarPorFiltros(PesquisaDTO pesquisa){
    }
    

    另外PesquisaDTO需要对每个字段设置/获取方法,然后Spring会将http get参数转换为PesquisaDTO对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 2014-02-08
      • 2018-07-31
      • 2017-08-16
      • 2015-11-11
      • 1970-01-01
      相关资源
      最近更新 更多