![](/img/trans.png)
[英]return the number of connections on websocket server based on jetty 8
[英]Jetty server cannot return json
当我使用mvn jetty:run启动我的服务器时,它无法返回json格式的字符串,它似乎仅对所有对象返回原始字符串或“ null”
我的端点代码,您看到的没有什么区别,只不过是在包装的对象中返回字符串并以原始字符串形式返回
@Path("/snapsono/")
public class RestEndPoint {
GeneralServer server = new GeneralImpl();
private Logger logger = Logger.getLogger("RestEndPoint");
@GET
@Path("/hello")
@Produces({MediaType.APPLICATION_JSON})
public SnapString sayHello() throws Exception{
logger.info("reach hello");
SnapString snapString = new SnapString("hello");
logger.info("this is: "+snapString.getString());
return snapString;
}
@GET
@Path("/helloString")
@Produces({MediaType.APPLICATION_JSON})
public String sayHelloString() throws Exception{
return "hello";
}
}
对于SnapString类,我确实在类区域添加了@XmlRootElement。
@XmlRootElement
public class SnapString {
private String str = "";
public SnapString(){
}
public SnapString(String str){
this.str = str;
}
public String getString(){
return this.str;
}
}
但是当我运行它时,我得到以下回报
dilin-mbp:~ dilin$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:9090/snapsono/hello
HTTP/1.1 200 OK
Content-Type: application/json
Transfer-Encoding: chunked
Server: Jetty(8.1.16.v20140903)
null
dilin-mbp:~ dilcurl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:9090/snapsono/helloString
HTTP/1.1 200 OK
Content-Type: application/json
Transfer-Encoding: chunked
Server: Jetty(8.1.16.v20140903)
hello
dilin-mbp:~ dilin$
也就是说,可以返回原始字符串,但不能返回Json格式的对象,并且在将其包装后,它始终返回null。
同时,从日志中我可以看出返回对象确实具有值
[INFO] Restart completed at Mon Feb 16 21:53:56 PST 2015
Feb 16, 2015 9:54:48 PM snap.sono.demo.rest.RestEndPoint sayHello
INFO: reach hello
Feb 16, 2015 9:54:48 PM snap.sono.demo.rest.RestEndPoint sayHello
INFO: this is: hello
Feb 16, 2015 9:57:45 PM snap.sono.demo.rest.RestEndPoint sayHello
请检查我幼稚的实现以获取两个信息日志
整个码头启动消息是:
dilin-mbp:snapsono dilin$ mvn jetty:run
[INFO] Scanning for projects...
[WARNING] Failed to retrieve plugin descriptor for org.eclipse.jetty:jetty- server:8.1.8.v20121106: Failed to parse plugin descriptor for org.eclipse.jetty:jetty-server:8.1.8.v20121106 (/Users/dilin/.m2/repository/org/eclipse/jetty/jetty-server/8.1.8.v20121106/jetty- server-8.1.8.v20121106.jar): No plugin descriptor found at META- INF/maven/plugin.xml
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Sproject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] Failed to retrieve plugin descriptor for org.eclipse.jetty:jetty-server:8.1.8.v20121106: Failed to parse plugin descriptor for org.eclipse.jetty:jetty-server:8.1.8.v20121106 (/Users/dilin/.m2/repository/org/eclipse/jetty/jetty-server/8.1.8.v20121106/jetty- server-8.1.8.v20121106.jar): No plugin descriptor found at META- INF/maven/plugin.xml
[INFO]
[INFO] >>> jetty-maven-plugin:8.1.8.v20121106:run (default-cli) > test-compile @ Sproject >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Sproject ---
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/dilin/Documents/workspace/snapsono/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ Sproject ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Sproject ---
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/dilin/Documents/workspace/snapsono/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ Sproject ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< jetty-maven-plugin:8.1.8.v20121106:run (default-cli) < test-compile @ Sproject <<<
[INFO]
[INFO] --- jetty-maven-plugin:8.1.8.v20121106:run (default-cli) @ Sproject ---
[INFO] Configuring Jetty for project: Sproject
[INFO] webAppSourceDirectory not set. Defaulting to /Users/dilin/Documents/workspace/snapsono/src/main/webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = /Users/dilin/Documents/workspace/snapsono/target/classes
[INFO] Context path = /
[INFO] Tmp directory = /Users/dilin/Documents/workspace/snapsono/target/tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] web.xml file = file:/Users/dilin/Documents/workspace/snapsono/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = /Users/dilin/Documents/workspace/snapsono/src/main/webapp
2015-02-16 22:00:01.766:INFO:oejs.Server:jetty-8.1.8.v20121106
2015-02-16 22:00:02.128:INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp requires one, please configure one.
Null identity service, trying login service: null
Finding identity service: null
2015-02-16 22:00:03.549:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/Users/dilin/Documents/workspace/snapsono/src/main/webapp/},file:/Users/dilin/Documents/workspace/snapsono/src/main/webapp/
2015-02-16 22:00:03.549:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/Users/dilin/Documents/workspace/snapsono/src/main/webapp/},file:/Users/dilin/Documents/workspace/snapsono/src/main/webapp/
2015-02-16 22:00:03.549:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/Users/dilin/Documents/workspace/snapsono/src/main/webapp/},file:/Users/dilin/Documents/workspace/snapsono/src/main/webapp/
Feb 16, 2015 10:00:03 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
snap.sono.demo.rest
Feb 16, 2015 10:00:03 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
class snap.sono.demo.rest.RestEndPoint
Feb 16, 2015 10:00:03 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Feb 16, 2015 10:00:03 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.8 06/24/2011 12:17 PM'
2015-02-16 22:00:04.040:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:9090
[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 40 seconds.
我终于解决了这个问题。
也就是说,在类级别,我们需要标记为@XmlRootElement
然后,对于每个return属性,我们需要将其标记为@XmlElement,以便可以以json格式返回被标记的成员变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.