-source 1.5中不支持java ee websocket chatroom try-with-resources

时间:2016-01-19 17:55:23

标签: java websocket

  

错误消息:-source 1.5

中不支持try-with-resources

完整的错误消息:

  

\应用\ workspace_eclipse \ EmpressFinance \ SRC \主\的java \ COM \皇后\ JPMC \聊天\ chatroom.java:[58,5]   错误:-source 1.5

中不支持try-with-resources

代码:

private String buildJsonData(String username, String message) {

        JsonObject jsonobject = Json.createObjectBuilder().add("message",username+ ": "+message).build();
        StringWriter stringWritter = new StringWriter();
        try(JsonWriter jsonwritter = Json.createWriter(stringWritter)) 
        {
            jsonwritter.write(jsonobject);
        }

        return stringWritter.toString();
    }

java的完整代码:

package com.empress.jpmc.chat;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set; 

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonWriter;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;


@ServerEndpoint("/chatroomServerEndpoint")
public class chatroom {
    static Set<Session> chatroomUser = Collections.synchronizedSet(new HashSet<Session>());

    @OnOpen
    public void handleOpen(Session userSession)
    {
        chatroomUser.add(userSession);
    }

    @OnClose
    public void handleClose(Session userSession)
    {
        chatroomUser.remove(userSession);
    }

    @OnMessage
    public void handleMessage(String message, Session userSession) throws IOException
    {
        String username = (String) userSession.getUserProperties().get("username");

        if (username==null)
        {
            userSession.getUserProperties().put("username", message);
            userSession.getBasicRemote().sendText(buildJsonData("System","You are connected as" + message ));
        } 
        else
        {
            Iterator<Session> iterator = chatroomUser.iterator();
            while (iterator.hasNext()) iterator.next().getBasicRemote().sendText(buildJsonData(username, message));
        }

    }

    private String buildJsonData(String username, String message) {

        JsonObject jsonobject = Json.createObjectBuilder().add("message",username+ ": "+message).build();
        StringWriter stringWritter = new StringWriter();
        try(JsonWriter jsonwritter = Json.createWriter(stringWritter)) 
        {
            jsonwritter.write(jsonobject);
        }

        return stringWritter.toString();
    }
}

2 个答案:

答案 0 :(得分:0)

 <properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>

将此代码段添加到pom.xml将解决此问题。

答案 1 :(得分:0)

是的,vinoth10对! pom.xml必须包含以下内容:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>7</source>
                <target>7</target>
            </configuration>
        </plugin>
    </plugins>
</build>