Ido*_*o_f 8 java rest json jersey maven
我花了太多时间(我会说超过10个)试图找出如何获得基本的json调用(来自angularjs)以打开并处理我的Jersey 2.4.我已经尝试了谷歌的每一个可能的结果,我仍然得到一个
415(不支持的媒体类型)
客户端和
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:找不到媒体类型= application/json的MessageBodyReader,type = class jersey.webreadability.controllers.jsonmodels.TextInput,genericType = class jersey.webreadability.controllers.jsonmodels.TextInput.
在服务器端.
我会在这里写下我试图解决的每一个可能的文件,希望它能帮助别人帮助我.目前我并不在乎它将如何工作,只要它能够工作,我明白我应该让这个与Jackson或Gson一起工作的人.
依赖关系(来自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>
Run Code Online (Sandbox Code Playgroud)
来自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.jackson.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>
Run Code Online (Sandbox Code Playgroud)
对象类:
@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 +
'}';
}
}
Run Code Online (Sandbox Code Playgroud)
主要课程:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
和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!');
});
Run Code Online (Sandbox Code Playgroud)
这就是我现在所拥有的,我希望它有所帮助.
非常感谢你.
Ido*_*o_f 11
我将此添加为我自己的答案,因为我认为这将有助于任何人在将来拥有可怕的泽西岛工作副本.与此同时,@ Michal Gajdos的回答非常有用,你也应该在将来遇到问题时与他的文本联系起来.
最终帮助我的是一个依赖,其中包括你需要的几乎所有东西.只需添加它并尝试一下
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.2.3</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
来源:Git
删除两者jersey-media-json(此模块在Jersey 2.x中不再存在)和jersey-json(仅适用于Jersey 1.x)并添加其中一个模块jersey-media-moxy(JAXB)或jersey-media-json-jackson(POJO).有关这些模块的更多信息,请参见Jersey用户指南中的JSON章节.
删除com.sun.jersey.config.property.packages和com.sun.jersey.api.json.POJOMappingFeature它们新泽西1.x的具体和有泽西2.x中没有用 第一个已被替换,jersey.config.server.provider.packages第二个已被删除(使用POJO JSON < - >对象映射使用Jackson模块jersey-media-json-jackson).
如果您正在使用jersey-media-json-jackson模块,则需要在JAX-RS应用程序的扩展中或在(使用ServerProperties中的属性)中注册JacksonFeature - 请参阅Jackson章节.当模块在类路径上时,MOXy()会自动注册.web.xmljersey-media-moxy
| 归档时间: |
|
| 查看次数: |
15527 次 |
| 最近记录: |