1

我正在测试我实施的一项新的休息服务。该服务通过@ResponseBody 返回一个 Json。在每个控制器中,我返回相同的结构:

-代码

-信息

- 对象列表

我返回的对象是带有列表的消息,该列表扩展到其父类以获取代码和消息。这些是我的课:

private class SimpleMessage{
        ResponseCode code;
        String message;
        public ResponseCode getCode() {
            return code;
        }
        public void setCode(ResponseCode code) {
            this.code = code;
        }
        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
        public void setCodeAndMessage(ResponseCode code, String message){
            this.code = code;
            this.message = message;
        }
    }

private class MessageResponse<T> extends SimpleMessage{
    List<T> list;
    public List<T> getList() {
        return list;
    }
    public void setList(List<T> list) {
        this.list = list;
    }
}   

如果我尝试返回 SimpleMessage,它工作正常。但问题与 MessageResponse 类有关。例如,如果我在列表中返回带有类 User 的实例的此类,它将在无限循环中运行,直到我停止 tomcat 服务器。

这是我的控制器

public @ResponseBody SimpleMessage isUserSuscribed(@RequestBody String data){
    MessageResponse<User> msg = new MessageResponse<User>();
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    JsonNode node;
    try {
        node = mapper.readTree(data);
        BasicUser manager = mapper.convertValue(node.get("manager"), BasicUser.class); 
        User user = userService.getUserByOpenid(mapper.convertValue(node.get("openid"), String.class));
        msg.setList(Lists.newArrayList(user));
    } catch (JsonProcessingException e) {
        log.debug(e.toString());
        msg.setCodeAndMessage(ResponseCode.error, "Malformed JSON \n" + e.getMessage());
    } catch (IOException e1) {
        log.debug(e1.toString());
        msg.setCodeAndMessage(ResponseCode.error, "Application error");
    }   
    msg.setCodeAndMessage(ResponseCode.success, "success");
    return msg;
}

用户类:

public class User extends AbstractPropertySearcher{

    private String username;

    private String password;

    private String email;

    private Boolean active;

    private String metadata;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

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

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public String getMetadata() {
        return getMeta().getMetadata();
    }

    public void setMetadata(String metadata) {
        this.metadata = metadata;
    }

    public enum KnownUsers{
        TEST, SYSTEM
    }

    public User() {
        this.roles = new HashSet<String>();
        this.profiles = new HashSet<String>();
        this.geoareas = new HashSet<String>();
        this.properties = new HashSet<Property>();
    }

    public User(String username) {
        this.username = username;
        this.roles = new HashSet<String>();
        this.profiles = new HashSet<String>();
        this.geoareas = new HashSet<String>();
        this.properties = new HashSet<Property>();
    }

    public User(User o) {
        try{
            PropertyUtils.copyProperties(this, o);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Set<String> roles;

    private Set<String> profiles;

    private Set<String> geoareas;

    public Set<String> getRoles() {
        return roles;
    }
    public void setRoles(Set<String> roles) {
        this.roles = roles;
    }
    public Set<String> getProfiles() {
        return profiles;
    }
    public void setProfiles(Set<String> profiles) {
        this.profiles = profiles;
    }
    public Set<String> getGeoareas() {
        return geoareas;
    }
    public void setGeoareas(Set<String> geoareas) {
        this.geoareas = geoareas;
    }

    private Set<Property> properties;
    private Map<String,Property> mappedProperties;

    public Map<String,Property> getMappedProperties(){
        if(mappedProperties==null){
            mappedProperties = new HashMap<String,Property>();
            for(Property prop : getProperties()){
                mappedProperties.put(prop.getProperty(),prop);
            }           
        }
        return mappedProperties;
    }

    public Property getPropertyByName(KnownProperty knownProperty) throws PropertyNotFoundException{
        return getPropertyByName(knownProperty.getPropertyName());
    }

    public void setProperty(Property property){
        setProperty(properties, property);
    }

    public boolean hasRole(Role.KnownRoles role){
        return roles.contains(role.name());
    }

    public Set<Property> getProperties() {
        return properties;
    }
    public void setProperties(Set<Property> properties) {
        this.properties = properties;
    }

    public String toString(){
        return getUsername();
    }

    public int hashCode(){
        return toString().hashCode();
    }

    public boolean equals(Object o){
        if(o instanceof User){
            return getUsername().equals(((User) o).getUsername());
        }
        return false;
    }

    public Property getBannerProperty() throws PropertyNotFoundException{
        return AbstractPropertySearcher.getPropertyByName(getProperties(), KnownProperty.BANNER.getPropertyName());
    }

    private Metadata meta;

    public Metadata getMeta(){
        if(meta == null){
            meta = new Metadata(metadata);
        }
        return meta;
    }

    public enum KnownMetaProperty{
        REGISTRATION_DATE, LAST_ACCESS_DATE
    }

    public String getRegistrationDate(){
        return getMeta().getVariable(KnownMetaProperty.REGISTRATION_DATE.name());
    }

    public String getLastAccessDate(){
        return getMeta().getVariable(KnownMetaProperty.LAST_ACCESS_DATE.name());
    }

}

这是日志中的错误:

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.StackOverflowError

有人可以帮我解决这个问题吗?谢谢

4

0 回答 0