Java Jersey Json 和 Pojo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19967373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jersey Json and Pojo
提问by Ido_f
I've spent far too many hours (over 10 i would say) trying to figure out how to get basic json calls (from angularjs) to open and process on my Jersey 2.4. I've tried every possible result on google and am still getting a
我花了太多时间(我会说超过 10 个)试图弄清楚如何让基本的 json 调用(来自 angularjs)在我的 Jersey 2.4 上打开和处理。我已经在谷歌上尝试了所有可能的结果,但仍然得到一个
415 (Unsupported Media Type)
415(不支持的媒体类型)
client side and a
客户端和一个
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=class jersey.webreadability.controllers.jsonmodels.TextInput, genericType=class jersey.webreadability.controllers.jsonmodels.TextInput.
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:找不到媒体类型=应用程序/json、类型=类 jersey.webreadability.controllers.jsonmodels.TextInput、genericType=class jersey.webreadability.controllers.jsonmodels.TextInput 的 MessageBodyReader。
on the server side.
在服务器端。
I'll write here every possible file i've changed while trying to solve this, with the hope it will help someone to help me. At the moment i don't really care how will it work as long as it will work, i do understand whoever that i should probably get this working with either Hymanson or Gson.
我会在这里写下我在尝试解决此问题时更改过的所有可能文件,希望它能帮助别人帮助我。目前我并不真正关心它会如何工作,只要它会起作用,我确实理解我应该与 Hymanson 或 Gson 一起工作的人。
Dependencies (from the POM File):
依赖项(来自 POM 文件):
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json</artifactId>
<version>2.0-m05-1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.17.1</version>
</dependency>
from the Web.xml:
来自 Web.xml:
<servlet>
<servlet-name>webReadability</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>jersey.webreadability.controllers</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.algo.server.webservice;org.codehaus.Hymanson.jaxrs</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
The object class:
对象类:
@XmlRootElement(name = "TextInput")
public class TextInput implements Serializable {
@XmlElement public String url;
@XmlElement public String text;
@XmlElement public String file;
public TextInput() {
}
public TextInput(String url, String text, String file) {
this.url = url;
this.text = text;
this.file = file;
}
@Override
public String toString() {
return "TextInput{" + "url=" + url + ", text=" + text + ", file=" + file +
'}';
}
}
main class:
主类:
@Path("/analysisController")
public class AnalysisController {
@POST
@Path("/sayHello")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public String sayHello(final TextInput text) {
System.out.println("printing out the sent text: " + text);
return "hello " + text.file;
}
}
and js:
和js:
var mockDataForThisTest = "json=" + encodeURI(JSON.stringify([
{
url: "http://www.example.com",
text: "txext text tex",
file: $scope.textarea
}
]));
var httpRequest = $http({
method: 'POST',
url: '/rest/analysisController/sayHello/',
data: mockDataForThisTest,
headers: {
'Content-Type': 'application/json'
}
}).success(function(data, status) {
$scope.textarea = data;
console.log(data);
}).error(function(error) {
console.log('error!');
});
That's all I currently have, I hope it helps.
目前就这些,希望对大家有帮助。
Thank you so very much.
非常感谢你。
采纳答案by Ido_f
I'm adding this as my own answer because i think this will help anyone with a horrible working copy of Jersey in the future. At the same time, the answer of @Michal Gajdos was grealy helpful and you should also relate to his text when having problems in the future.
我将此添加为我自己的答案,因为我认为这将有助于将来拥有糟糕的 Jersey 工作副本的任何人。同时,@Michal Gajdos 的回答非常有帮助,以后遇到问题时也应该参考他的文字。
What eventually helped me was a dependecy which includes pretty much every thing you need. simply add it and give it a try
最终帮助我的是一个包含几乎所有你需要的东西的依赖。只需添加它并尝试一下
<dependency>
<groupId>com.fasterxml.Hymanson.jaxrs</groupId>
<artifactId>Hymanson-jaxrs-json-provider</artifactId>
<version>2.2.3</version>
</dependency>
source: Git
来源:Git
回答by Michal Gajdos
Dependencies
依赖关系
Remove both jersey-media-json
(this module doesn't exist any more in Jersey 2.x) and jersey-json
(which is only for Jersey 1.x) and add one of the modules jersey-media-moxy
(JAXB) or jersey-media-json-Hymanson
(POJO). See JSON chapterin Jersey User Guide for more information on these modules.
删除两者jersey-media-json
(此模块在 Jersey 2.x 中不再存在)和jersey-json
(仅适用于 Jersey 1.x)并添加模块jersey-media-moxy
(JAXB) 或jersey-media-json-Hymanson
(POJO) 之一。有关这些模块的更多信息,请参阅Jersey 用户指南中的JSON 章节。
web.xml
网页.xml
Remove com.sun.jersey.config.property.packages
and com.sun.jersey.api.json.POJOMappingFeature
which are Jersey 1.x specific and have no use in Jersey 2.x. The first one has been replaced with jersey.config.server.provider.packages
and the second one has been removed (to use POJO JSON<->Object mapping use Hymanson module jersey-media-json-Hymanson
).
删除com.sun.jersey.config.property.packages
和com.sun.jersey.api.json.POJOMappingFeature
哪些是 Jersey 1.x 特定的并且在 Jersey 2.x 中没有用。第一个已被替换,jersey.config.server.provider.packages
第二个已被删除(使用 POJO JSON<->Object mapping use Hymanson module jersey-media-json-Hymanson
)。
Register features
注册功能
If you're using jersey-media-json-Hymanson
module you need to register HymansonFeaturein an extension of JAX-RS application or in web.xml
(using properties from ServerProperties) - see Hymanson chapter. MOXy (jersey-media-moxy
) is registered automatically when the module is on the class-path.
如果您正在使用jersey-media-json-Hymanson
模块,则需要在 JAX-RS 应用程序的扩展中或(使用ServerProperties 中的属性)中注册HymansonFeature- 请参阅Hymanson 章节。当模块在类路径上时会自动注册MOXy ( )。web.xml
jersey-media-moxy
回答by Ben Asmussen
Enable HymansonFeature within web.xml. See also ServerProperties
在 web.xml 中启用 HymansonFeature。另请参阅服务器属性
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.your.packages</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.Hymanson.HymansonFeature</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>