简体   繁体   中英

Configurable websocket url

I've a websocket endpoint implemented as:

@ServerEndpoint(
    value="/foo/{id}",
    encoders = { MyEncoder.class },
    decoders = { MyDecoder.class },
    subprotocols = { "foo" }
)
public class MyEndPoint {
...
}

it is working ok. However, I need to move the url /foo/{id} to a config file (lets say web.xml) instead of hard-coded in the value of the @ServerEndpoint annotation.

Any hint? Thanks.

I think it's a similar problem with here (about Request Mapping annotations).

Anyway configuration would be like as follows.

  • [MyEndPoint.java]

     @ServerEndpoint( value="${websocket.base_uri}/{id}", // --> `id` is a variable name so it's NOT suit to customize it in config file. encoders = { MyEncoder.class }, decoders = { MyDecoder.class }, subprotocols = { "${websocket.subprotocol}" } ) public class MyEndPoint { private long id; ... }
  • [websocket.properties]

     websocket.base_uri=/foo websocket.subprotocol=foo
  • [spring-context.xml]

     <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>websocket.properties</value> </property> </bean>... </beans>
  • [web.xml]

     <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="..." version="3.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-context.xml</param-value> </context-param>... </web-app>

I don't know to include the PathVaraible name - {id} - in config file, and also think that it isn't suit to configurate the variable name in both xml and java files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM