gpt4 book ai didi

java - 如何通过 angularJS 将 java 对象发送到 get 方法

转载 作者:行者123 更新时间:2023-12-01 22:51:25 27 4
gpt4 key购买 nike

我必须在 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()

这是 Angular 函数:

 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; }'.

有人可以帮助我吗?

最佳答案

在服务器端:

@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});

关于java - 如何通过 angularJS 将 java 对象发送到 get 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58456165/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com