0

I've come up against a problem. i can't properly use BigDecimal in my own class, to do post actions. hope for advice Using Jersey 2.x, Jackson 1.8

my class

public final class TestModel{

    private BigDecimal ffff;

    public BigDecimal getFfff() {
        return ffff;
    }

    public void setFfff(BigDecimal ffff) {
        this.ffff = ffff;
    }

    public void setFfff(double ffff) {
        this.ffff = new BigDecimal(ffff).setScale(2, RoundingMode.HALF_EVEN);
    }

}

and trying to do simple post action

@POST
@Path("/test")
@Consumes(MediaType.APPLICATION_JSON)
public Response test(TestModel tt) {
    log.entry();
    log.info(tt);               
    log.exit();
    return Response.status(201).entity("").build();
}

but in result i get {ClientResponse{method=POST, uri=http://somehost:8080/test, status=400, reason=Bad Request}

Get methods working fine. If I try to make post for BigDecimal that also works, problem came out when i try to put BigDecimal in my own class and than try to do post with it

Yasin Okumuş
  • 2,299
  • 7
  • 31
  • 62
  • Where is `TestModel` supposed to come from? Is is a path parameter, query parameter or form parameter. You need to tell Jersey where to get it from. Also check the json value you're posting to make sure it's properly formed. Finally, register a `LoggingFilter` with Jersey so you can trace exactly what's going on. – Baldy Aug 01 '14 at 12:20
  • [link = exmaple](http://examples.javacodegeeks.com/enterprise-java/rest/jersey/json-example-with-jersey-jackson/) .I followed this example there aren`t any definition of post parameter. is it necessery to use annotations & why thanks for logginFilter going to check – Bohdan Kloderic Aug 01 '14 at 12:56
  • Huh, learn something new every day. I've never used use `@POST` like that before. Thanks for educating me! – Baldy Aug 01 '14 at 12:58

1 Answers1

0

problem solved. problem was that, i have 2 setter methods. and when i'm doing post action jackson deserilize variable into BigDecimal but trying to use setter for double. solution is to rename setter for double or use own deserilization

public class TestDeser extends JsonDeserializer<Double> {
    @Override
    public Double deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
        return jp.getDoubleValue();      
    }

and in model class

 @JsonDeserialize(using = TestDeser.class)
public void setCheat(double cheat) {
    this.cheat = new BigDecimal(cheat).setScale(2,RoundingMode.HALF_EVEN);
}

but i don't know what solution is better.