Java Jetty服务器无法返回json

Java Jetty服务器无法返回json,java,json,rest,maven,jetty,Java,Json,Rest,Maven,Jetty,当我使用mvnjetty:run启动我的服务器时,它不能返回json格式的字符串,它似乎只为所有对象返回原始字符串或null 在我的端点代码中,除了返回包装对象中的字符串并作为原始字符串返回外,并没有什么区别 @Path("/snapsono/") public class RestEndPoint { GeneralServer server = new GeneralImpl(); private Logger logger = Logger.getLog

当我使用mvnjetty: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

但当我运行它时,我得到了以下回报

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


然后,对于每个返回属性,我们需要将其标记为@XmlElement,这样标记的成员变量就可以以json格式返回。

我在jetty 9中也尝试过同样的方法。成功了。我收到了snapsono的{string:hello}/hello
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.