gpt4 book ai didi

java - Angular 2 Jax-RS 的 GET 比 POST 更快

转载 作者:太空宇宙 更新时间:2023-11-04 11:23:08 28 4
gpt4 key购买 nike

我有一个使用 Jax-RS 和 Angular 2、Apache 8.5 和使用 Hibernate 的 Postgres 数据库的 REST 应用程序。

问题是当我保存新对象时,列表中的 GET 在 POST 完全运行之前运行。这是代码。

tabela-servicos.component.ts

import { Component, OnInit } from '@angular/core';
import { Servico } from '../servico';
import { CrudServicosService } from '../crud-servicos.service';

@Component({
selector: 'app-tabela-servicos',
templateUrl: './tabela-servicos.component.html',
styleUrls: ['./tabela-servicos.component.css']
})
export class TabelaServicosComponent implements OnInit {

titulo = "Tabela de Serviços";
servicos: Servico[] = [];

constructor(private servicoService: CrudServicosService) {

}

ngOnInit() {
console.log('called ngOnInit')
this.servicoService.getServicos()
.subscribe(s => {
console.log(s);
this.servicos = s;
}, erro => console.log(erro));
}

remover(servico: Servico){
this.servicoService.removerServico(servico)
.subscribe(() => {
let novosServicos = this.servicos.slice(0);
let indice = novosServicos.indexOf(servico);
novosServicos.splice(indice, 1);
this.servicos = novosServicos;
});
}
}

form-servicos.component.ts

import { Component, OnInit } from '@angular/core';
import { CrudServicosService } from '../../app/crud-servicos.service';
import { Servico } from '../../app/servico';
import { Router, ActivatedRoute } from '@angular/router';

@Component({
selector: 'app-form-servicos',
templateUrl: './form-servicos.component.html',
styleUrls: ['./form-servicos.component.css']
})
export class FormServicosComponent implements OnInit {

titulo = "Cadastro de Servicos";
servico: Servico;
codigo;

constructor(private servicoService: CrudServicosService,
private router: Router,
private rota:ActivatedRoute) { }

ngOnInit() {
this.codigo = this.rota.snapshot.params['cod'];
if(isNaN(this.codigo)){
this.servico = new Servico();
} else {
this.servico = Object.assign({},
this.servicoService.getServicoPorCodigo(this.codigo));
}
}

salvarServico(){
if(isNaN(this.codigo)){
this.servicoService.adicionarServico(this.servico)
} else {
this.servicoService.atualizaServico(this.codigo, this.servico);
}
// console.log("waiting 2 seconds to return page list...");
// setTimeout(()=>{
this.router.navigate(['/lista']);
// },2000);
}

cancelar(){
this.router.navigate(['/lista']);
}
}

ServicoResource.java

package resource;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.google.gson.Gson;

import dao.ServicoDAO;
import model.Servico;

@Path("servico")
public class ServicoResource {

private static ServicoDAO servicoDao = new ServicoDAO();

@GET
@Path("servicos")
@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
public Response test() {
List<Servico> serv = servicoDao.getList();
System.out.println("GET Objectos from Jax-RS");
System.out.println("");
return Response.ok(serv).build();
}

@Path("servicos/delete/{id}")
@DELETE
public Response removeProduto(@PathParam("id") long id){
System.out.println("DELETE Object ID: " + id + " Jax-RS ");
System.out.println("");
servicoDao.remover(id);
return Response.ok().build();
}

@POST//consume o objeto enviado pro back-end
@Consumes(MediaType.APPLICATION_JSON)
public Response adiciona(String conteudo){
Gson gson = new Gson();
Servico s = gson.fromJson(conteudo, Servico.class);
servicoDao.salvar(s);
System.out.println("POST Object Jax-RS");
System.out.println("");
return Response.status(200).build();
}
}

prints from ServerLog, ConsoleBrowser and Application

当我重新加载列表页面时,它会出现在保存的对象之后。当我在 form-servicos.component.ts 类中使用下面的代码时,它可以工作,但我知道这是一个不好的做法。

 console.log ("waiting 2 seconds to return page list ...");
setTimeout (() => {
This.router.navigate (['/ list']);
}, 2000);

有谁知道如何在持久化对象之前从请求的列表中解决这个问题???

最佳答案

(代表 OP 发布)

问题已解决。

adicionarServico(servico: Servico){
console.log("Called persist POST "+JSON.stringify(servico));
return this.http.post(this.baseUrl + '/servico', JSON.stringify(servico), {headers: this.headers});
}

我更改了 crud-service.ts 以返回 Observable < Responde > 并将其用于:

salvarServico(){
this.servicoService.adicionarServico(this.servico)
.subscribe(() => {
console.log("Saved Success");
this.router.navigate(['/lista']);
});
}

关于java - Angular 2 Jax-RS 的 GET 比 POST 更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44691303/

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