gpt4 book ai didi

java - Jetty服务器无法返回json

转载 作者:太空宇宙 更新时间:2023-11-04 14:09:04 25 4
gpt4 key购买 nike

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

请检查我对两个信息日志的幼稚实现

整个 jetty 启动消息是:

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格式返回。

关于java - Jetty服务器无法返回json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28555899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com