Send json to webservice and return status

Asked

Viewed 181 times

1

Guys I’m doing a course of Caelum 36 but I stopped at some point I need to do this:

curl -i -H "Content-type: application/json" -X POST -d '{"valor":"39.9","titular":"Fulano"}' http://localhost:8080/fj36-webservi
ce/pagamento

Only at the terminal it’s turning me on

HTTP/1.1 400 Bad Request
Connection: keep-alive
X-Powered-By: Undertow/1
Server: WildFly/8
Content-Type: text/html
Content-Length: 257
Date: Tue, 05 Sep 2017 21:35:40 GMT

What do I do to make it work? I don’t know how to fix it. It’s on page 98 (who has the handout)

Error that appears in terminal:

com.fasterxml.jackson.core.JsonParseException: Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: io.undertow.servlet.spec.ServletInputStreamImpl@72f1de92; line: 1, column: 2]

Class: Paymentresource

package br.com.caelum.payfast.rest;

import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import javax.ejb.Stateless;
import javax.ws.rs.Consumes;
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 br.com.caelum.payfast.modelo.Pagamento;
import br.com.caelum.payfast.modelo.Transacao;

@Path("/pagamento")
@Stateless
public class PagamentoResource {
    private Map<Integer, Pagamento> repositorio = new HashMap<>();
    private Integer idPagamento = 1;

    @POST
    @Consumes ({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response criarPagamento(Transacao transacao) throws URISyntaxException {
        Pagamento pagamento = new Pagamento();
        pagamento.setId(idPagamento++);
        pagamento.setValor(transacao.getValor());
        pagamento.comStatusCriado();

        repositorio.put(pagamento.getId(), pagamento);

        System.out.println("PAGAMENTO CRIADO " + pagamento);

        return Response.created(new URI("/pagamento/" + pagamento.getId()))
                .entity(pagamento)
                    .type(MediaType.APPLICATION_JSON_TYPE)
                        .build();
    }

    public PagamentoResource() {
        Pagamento pagamento = new Pagamento();
        pagamento.setId(idPagamento++);
        pagamento.setValor(BigDecimal.TEN);
        pagamento.comStatusCriado();
        repositorio.put(pagamento.getId(), pagamento);
    }

    @GET
    @Path("/{id}")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.applica}) // cuidado javax.ws.rs
    public Pagamento buscaPagamento(@PathParam("id") Integer id) {
        return repositorio.get(id);
    }   

}

Class: Payment service

package br.com.caelum.payfast.rest;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/")
public class PagamentoService extends Application {

}

Class: Transaction

package br.com.caelum.payfast.modelo;

import java.math.BigDecimal;


public class Transacao {

    private String numero;
    private String titular;
    private String data;
    private BigDecimal valor;

    public void setNumero(String numero) {
        this.numero = numero;
    }

    public void setData(String data) {
        this.data = data;
    }

    public void setValor(BigDecimal valor) {
        this.valor = valor;
    }

    public void setTitular(String titular) {
        this.titular = titular;
    }

    public String getNumero() {
        return numero;
    }

    public String getTitular() {
        return titular;
    }

    public String getData() {
        return data;
    }

    public BigDecimal getValor() {
        return valor;
    }

    @Override
    public String toString() {
        return "Transacao [numero=" + numero + ", titular=" + titular + ", data=" + data + ", valor="
                + valor + "]";
    }

}
  • Classed Transacao along with your question. I believe the problem may be in the attribute valor being passed off as a String for the appeal.

  • Okay, I’ve included the Transacao class. Take a look.

1 answer

0

Your problem is originated by the fact that you are sending a String for an attribute waiting for a BigDecimal, which in your case is valor.

Where do you send:

curl -i -H "Content-type: application/json" -X POST -d '{"valor":"39.9","titular":"Fulano"}' http://localhost:8080/fj36-webservice/pagamento

Note that the attribute valor is represented as follows: "valor":"39.9", when he should be: "valor":39.9, without the quotation marks.

Thus, your request should be:

curl -i -H "Content-type: application/json" -X POST -d '{"valor":39.9,"titular":"Fulano"}' http://localhost:8080/fj36-webservice/pagamento
  • Making a mistake.

  • The @GET class PagamentoResource is working?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.