JAX-RS消耗json问题

时间:2015-06-29 12:24:31

标签: java json rest jersey jax-rs

我在java中遇到@Consumes JSON对象的编码问题。

我使用glassfish服务器4.1。

Json示例:

 {
   "name": "нцугкцукгцароцупацу",
   "email": "text@gmail.com",
   "message": "цкуцгкрпгукргшенгшукрпшгрук" 
 }

代码(来自我的网络应用):

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它是一个简单的pojo对象和jpa实体。

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

}

结果:

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

当我尝试这样的时候:

@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";
}

一切正常; 但我想使用json对象,并以某种方式为此对象设置编码(UTF-8我猜)... :) 我也试过

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

但没有改变:(

我会很高兴得到任何帮助。

1 个答案:

答案 0 :(得分:0)

嗯,你可以直接传递JSONObject,但你可以将它映射到DTO并传递

假设您的参数我有一个DTO,我将使用它而不是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";
}