JAX-RS requires json question

I have a problem with encoding @Consumes

a JSON object in java.

I am using Glassfish 4.1 server.

Json example:

 {
   "name": "",
   "email": "text@gmail.com",
   "message": "" 
 }

      

Code (from my web app):

public static final String ENCODING = ";charset=utf-8";

@POST
@Path("sendTest")
@Produces(MediaType.APPLICATION_JSON + ENCODING)
@Consumes(MediaType.APPLICATION_JSON + ENCODING)
public String sendTest(@Context HttpServletRequest request, JsonObject jsonObject ) {
    try {
        logger.info("encoding - " + request.getCharacterEncoding());
        logger.info("name - " + new String(jsonObject.getName().getBytes("UTF-8"), "UTF-8"));

        logger.info("msg - " + jsonObject.getMessage());
    } catch (Exception e) {
        logger.error(e);
        return "ERROR";
    }
    return "OK";
}

      

JsonObject is a simple pojo object and a jpa object.

    import java.io.Serializable;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @Entity
    @Table(name = "jsonObject")
    public class JsonObject implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    private String email;

    private String message;

    public JsonObject() {
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        JsonObject other = (JsonObject) obj;
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "JsonObject [id=" + id + ", name=" + name + ", email=" + email + ", message=" + message + "]";
    }

}

      

Result:

[ INFO] encoding - UTF-8.
[ INFO] name - ??????????.          
[ INFO] msg - ???????????.

      

When I try like this:

@POST
@Path("sendTestParam")
@Produces(MediaType.APPLICATION_JSON + ENCODING)
@Consumes(MediaType.APPLICATION_JSON + ENCODING)
public String sendTestParam(@Context HttpServletRequest request, @QueryParam("name") String name, @QueryParam("email") String email,
        @QueryParam("message") String message) {
    try {
        logger.info("encoding - " + request.getCharacterEncoding());
        logger.info("name - " + name);
        logger.info("email - " + email);
        logger.info("msg - " + message);
    } catch (Exception e) {
        logger.error(e);
        return "ERROR";
    }
    return "OK";
}

      

Everything works fine; But I want to use a json object and somehow set the encoding (UTF-8 I think) for that object ... :) I also tried

@Consumes(MediaType.APPLICATION_JSON)
@Consumes("application/json")
@Consumes("application/json;charset=utf-8")

      

but nothing has changed :(

I will appreciate any help.

+3


source to share


1 answer


Well you can pass the JSONObject directly, but you can map it to the DTO and pass

Assuming your parameters have a DTO and I will use that instead of JsonObject



class DummyTO {

    private String name;
    private String email;
    private String message;
    // getter setter
}

@POST
@Path("sendTest")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String sendTest(@Context HttpServletRequest request, DummyTO dummtTO ) {
    try {

        dummyTO.getMessage();


    } catch (Exception e) {
        logger.error(e);
        return "ERROR";
    }
    return "OK";
}

      

0


source







All Articles