我在看到POST请求时收到415-不支持的媒体类型。我检查了很多给出的解决方案,但似乎都没有。我将内容类型设置为application / json。但我仍然遇到同样的问题。
我尝试了添加jackson-all.jar,jersey-media-json-jakson jar和添加no-arg构造函数的解决方案。似乎无效。
图书资源
package com.jugo.own.rest.resources;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.jugo.own.database.dao.BookDAO;
import com.jugo.own.rest.services.BookRO;
@Path("book")
public class BookResource {
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public String getBook(@PathParam("id") Long id){
BookDAO book = new BookDAO();
return book.getBook(id);
}
@POST
@Path("/add")
@Consumes(MediaType.APPLICATION_JSON)
public void createBook(BookRO ro) {
BookDAO book = new BookDAO();
book.createBooks(ro);
}
}
BookRO
public class BookRO {
private String bookName;
private Long sectionId;
private Long id;
private String author;
private String publication;
public BookRO(){
}
public BookRO(String bookName, Long sectionId, Long id, String author, String publication) {
this.bookName = bookName;
this.sectionId = sectionId;
this.id = id;
this.author = author;
this.publication = publication;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public Long getSectionId() {
return sectionId;
}
public void setSectionId(Long sectionId) {
this.sectionId = sectionId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublication() {
return publication;
}
public void setPublication(String publication) {
this.publication = publication;
}
}
的web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>LibManagement</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey</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.vamsi.own.rest.resources</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
JSON请求
{
"bookName" : "Lord Of The Rings : The Fellowship Of The Ring",
"sectionId" : 1,
"id" : 1,
"author" : "J.R.R Tolkein",
"publication" : "Allen & Unwin"
}
另外,下面是我的jar依赖项。
aopalliance-repackaged-2.3.0-b10.jar
asm-debug-all-5.0.2.jar
com.fasterxml.jackson.core.jar
com.fasterxml.jackson.databind.jar
derby.jar
eclipselink.jar
hk2-api-2.3.0-b10.jar
hk2-locator-2.3.0-b10.jar
hk2-utils-2.3.0-b10.jar
jackson-all-1.9.0.jar
jackson-annotations-2.1.2.jar
jackson-jaxrs-base-2.2.3.jar
jackson-jaxrs-json-provider-2.5.4.jar
jackson-module-jaxb-annotations-2.2.2.jar
javassist-3.18.1-GA.jar
javax.annotation-api-1.2.jar
javax.inject-2.3.0-b10.jar
javax.persistence_2.1.0.v201304241213.jar
javax.persistence.source_2.1.0.v201304241213.jar
javax.servlet-api-3.0.1.jar
javax.ws.rs-api-2.0.1.jar
jaxb-api-2.2.7.jar
jersey-client.jar
jersey-common.jar
jersey-container-servlet-core.jar
jersey-container-servlet.jar
jersey-core-1.9.1.jar
jersey-guava-2.13.jar
jersey-media-json-jackson-2.0-m07-1.jar
jersey-media-multipart-2.13.jar
jersey-server.jar
mimepull.jar
org.eclipse.persistence.jpa.modelgen_2.5.2.v20140319-9ad6abd.jar
org.eclipse.persistence.jpa.modelgen.source_2.5.2.v20140319-9ad6abd.jar
org.eclipse.persistence.jpars_2.5.2.v20140319-9ad6abd.jar
org.eclipse.persistence.jpars.source_2.5.2.v20140319-9ad6abd.jar
org.osgi.core-4.2.0.jar
osgi-resource-locator-1.0.1.jar
persistence-api-1.0.jar
validation-api-1.1.0.Final.jar
有什么想法吗?
答案 0 :(得分:0)
在邮递员中,我遇到了同样的问题。
我的解决方案是将我的数据以json格式以[raw]格式写入[Body]。像这样:
{“消息”:“更新的第一篇文章!”}
在[标题]中:
[KEY]内容类型 [值] application / json