Send json to webservice and return status

1

I'm taking a course from Caelum 36 but I packed up at one 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 in the terminal is this turning me

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 work? I can not handle that mistake. You are on page 98 (who has the booklet)

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: PaymentService

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 + "]";
    }

}
    
asked by anonymous 05.09.2017 / 23:45

1 answer

0

Your problem is caused by the fact that you are sending String to an attribute that expects a BigDecimal , which in your case is valor .

Where you submit:

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

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

In this way, 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
    
07.09.2017 / 13:30