Jetty 服务器无法 return json
Jetty server cannot return json
当我使用 mvn jetty:run 启动我的服务器时,它不能 return json 格式化字符串,它似乎只能 return 原始字符串或 "null" 对于所有对象
我的端点代码,你看除了 return 包装对象中的字符串和 return 作为原始字符串
没有区别
@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 class,我确实在 class 区域添加了 @XmlRootElement
@XmlRootElement
public class SnapString {
private String str = "";
public SnapString(){
}
public SnapString(String str){
this.str = str;
}
public String getString(){
return this.str;
}
}
但是当我 运行 它时,我得到了以下 return
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$
也就是说,可以return一个原始字符串,但不能return一个Json格式的对象,当它被包裹时,它总是return null.
同时,从日志中我可以看出 return 对象确实具有值
[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.
我终于解决了这个问题。
也就是在class层我们需要标记为@XmlRootElement
然后对于每个return属性,我们需要将其标记为@XmlElement,这样被标记的成员变量就可以return编辑成json格式。
当我使用 mvn jetty:run 启动我的服务器时,它不能 return json 格式化字符串,它似乎只能 return 原始字符串或 "null" 对于所有对象
我的端点代码,你看除了 return 包装对象中的字符串和 return 作为原始字符串
没有区别 @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 class,我确实在 class 区域添加了 @XmlRootElement
@XmlRootElement
public class SnapString {
private String str = "";
public SnapString(){
}
public SnapString(String str){
this.str = str;
}
public String getString(){
return this.str;
}
}
但是当我 运行 它时,我得到了以下 return
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$
也就是说,可以return一个原始字符串,但不能return一个Json格式的对象,当它被包裹时,它总是return null.
同时,从日志中我可以看出 return 对象确实具有值
[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.
我终于解决了这个问题。
也就是在class层我们需要标记为@XmlRootElement
然后对于每个return属性,我们需要将其标记为@XmlElement,这样被标记的成员变量就可以return编辑成json格式。