javax.ws.rs.client.Entity json() to String
Asked Answered
C

0

7

I'm using Jersey to post some entities to a remote REST service through json, here is the client:

Invocation invocation = buildingWebTarget.request(MediaType.APPLICATION_JSON).
                        buildPut(Entity.json(tmpEntity));

at the other side I receive the entity but with all fields set to null. The tmpEntity has no null fields so I'm trying to debug what Entity.json() does, is there a way to print as String the result of Entity.json()??

Using

log.info(Entity.json(tmpEntity).toString())

only returns gibberish.

PS:

My tmpEntity is like:

@XmlRootElement
@Entity
public class City implements Serializable
{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }
}
Cota answered 29/1, 2016 at 14:30 Comment(8)
You can register the LoggingFilter with the client to see the request body, to see if it's even a problem with the client side. client.register(new LoggingFilter(Logger.getAnnonymousLogger(), true));Anon
sorry I dont' get it, what variable is client? client.register() what does it do...? By the way, I used wireshark to check that indeed it's the client doing wrong things by setting values to null after I used Proguard to compress the code of the client..Cota
Client client = ClientBuilder.newClient()... It's the Client instance variableAnon
What's your tmpEntity class look like? So are you able to get it to work in a standalone main() program. I'm not getting where Proguard fits into this. It almost sound like you're saying that Progaurd may be the cause.Anon
yes it's proguard mangling the jar and breaking something, if I don't use proguard on the resulting jar the code works correctly, I'm trying to understand where proguard is messing by logging each step to console.Cota
add default constructor to your entity public City(){ super();}. anyway i uses jackson to json converting utility. here example: public String toJson(Object input){ StringWriter sw = new StringWriter(); ObjectMapper mapper = new org.codehaus.jackson.map.ObjectMapper(); try { mapper.writeValue(sw, input); } catch (JsonGenerationException e) { log.error(e.getMessage(), e); } catch (JsonMappingException e) { log.error(e.getMessage(), e); } catch (IOException e) { log.error(e.getMessage(), e); } return sw.toString(); }Melancon
Ok I solved finally, I had to tell proguard to not process entity beans classes, an error returned by your toJson() function solved it: it returned Throwable caught: org.codehaus.jackson.map.JsonMappingException: Class my.app.beans.a.a has no default (no arg) constructor. If you post it as answer I'll accept it.Cota
You might also try to annotate your getters with @JsonProperty("id"). Jackson usually introspects bean properties. If proguards mangle them, then the reading is affected. If the properties are annotated, then it doesn't matter what the properties are mangled to, as the name in the annotation value will be used.Anon

© 2022 - 2024 — McMap. All rights reserved.