我正在尝试在@GET
来电中返回多方回复。无论我做什么,我总是收到同样的错误:
"无法在MultiPart实例上获取实体(通过参考链:org.glassfish.jersey.media.multipart.MultiPart [" entity"])"
我的jersey-media-multipart
中引用了pom.xml
。我试过添加引用,删除引用无济于事。我使用下面的(测试)代码创建Response
:
return Response.ok(new MultiPart()
.bodyPart("This is the first segment", new MediaType("text", "plain"))
.bodyPart("<outer><inner>value</inner></outer>", new MediaType("text", "xml"))).build();
我的方法注释为:
@GET
@Produces({"application/json","multipart/mixed"})
因为它在某些条件下也可以返回JSON。完全删除@Produces
或仅更改为"multipart/mixed"
不会更改错误消息。
我还有一个注册MultiPartFeature.class
的应用程序类。我必须补充一点,我在这个类中有一个@POST
方法,我使用MultiPart
来检索multipart/mixed
数据而没有问题。应用程序类的相关部分如下:
public class OpApplication extends ResourceConfig
{
public OpApplication()
{
// make the REST classes available
super(Resource classes listed here...);
// register the filters
register(AuthorizationRequestFilter.class);
register(AuthorizationResponseFilter.class);
// register the multipart package for attachments
packages("org.glassfish.jersey.examples.multipart");
register(MultiPartFeature.class);
register(JacksonFeature.class);
}
}
@GET方法:
@GET
@Produces({"application/json","multipart/mixed"})
public Response getData(@Context UriInfo ui)
{
boolean is_multipart = false;
// retrieve the parameter map
MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
ParameterValidator parameterValidator = new ParameterValidator();
// validate any existing parameters
if (!parameterValidator.getCallParametersAreValid(queryParams))
{
return Response.status(Response.Status.BAD_REQUEST).build();
}
else // check for the presence of parameter requesting a multipart response
{
is_multipart = parameterValidator.is_multipart();
}
JSONObject json = getDataAsJson(queryParams);
if (json == null || json.length() == 0)
{
return Response.status(Response.Status.NOT_FOUND).build();
}
else
{
// BodyParts would be gathered here. JSON and a file is most likely output
/* MultiPart objMultiPart = new MultiPart();
BodyPart coverageBodyPart = new BodyPart().
type(MediaType.APPLICATION_JSON_TYPE).
entity(json);
objMultiPart.getBodyParts().add(coverageBodyPart);
*/
if (is_multipart)
{
// simple test
return Response.ok(new MultiPart()
.bodyPart("This is the first segment", new MediaType("text", "plain"))
.bodyPart("<outer><inner>value</inner></outer>", new MediaType("text", "xml"))).build();
}
else
{
return Response.ok(json.toString(), MediaType.APPLICATION_JSON).build();
}
}
}
pom.xml
文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>opls</groupId>
<artifactId>oprest</artifactId>
<packaging>war</packaging>
<version>6.7.5</version>
<name>OPREST</name>
<properties>
<jdk.version>1.8</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- PROJECT SPECIFIC DEPENDENCIES -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>14.0.1</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.security</groupId>
<artifactId>oauth1-server</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.security</groupId>
<artifactId>oauth1-signature</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20151123</version>
</dependency>
<dependency>
<groupId>com.github.wnameless</groupId>
<artifactId>json-flattener</artifactId>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>osgi-resource-locator</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.1.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<finalName>oplrs-${project.version}</finalName>
<sourceDirectory>src/</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<webXml>web/WEB-INF/web.xml</webXml>
<webResources>
<resource>
<directory>web</directory>
</resource>
</webResources>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/</outputDirectory>
<resources>
<resource>
<directory>src/</directory>
<includes>
<include>*.ccf</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<debug>true</debug>
</configuration>
</plugin>
</plugins>
</build>
</project>
为了回应谢尔盖的代码示例(谢谢!),我创建了一个新的简单端点。它仍然会出现同样的错误:
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartMediaTypes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
@Path("/test")
public class Test
{
@GET
@Produces({MediaType.APPLICATION_JSON, MultiPartMediaTypes.MULTIPART_MIXED})
public Response getData(@Context UriInfo ui) {
final boolean condition = false;
if (condition) {
return Response.ok("{}", MediaType.APPLICATION_JSON).build();
}
return Response.ok(
new MultiPart()
.bodyPart("This is the first segment", MediaType.TEXT_PLAIN_TYPE)
.bodyPart("<outer><inner>value</inner></outer>", MediaType.TEXT_XML_TYPE),
MultiPartMediaTypes.MULTIPART_MIXED_TYPE
).build();
}
}
答案 0 :(得分:0)
根本原因是缺少Response.ok()
方法调用的显式媒体类型规范。
以下示例是使用Jetty作为HTTP服务器和Java Servlet容器的应用程序(JAR
)。
pom.xml
<project ...>
...
<packaging>jar</packaging>
<properties>
<jersey.version>2.25.1</jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
</project>
Program
class import java.net.URI;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.jetty.JettyHttpContainerFactory;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;
public class Program {
public static void main(final String[] args) {
final URI uri = URI.create("http://0.0.0.0:8080/");
final ResourceConfig resourceConfig = new ResourceConfig(TestResource.class);
resourceConfig.register(MultiPartFeature.class);
resourceConfig.register(JacksonFeature.class);
JettyHttpContainerFactory.createServer(uri, resourceConfig);
}
}
TestResource
class import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartMediaTypes;
@Path("/")
public class TestResource {
@GET
@Produces({MediaType.APPLICATION_JSON, MultiPartMediaTypes.MULTIPART_MIXED})
public Response getData(@Context UriInfo ui) {
final boolean condition = false;
if (condition) {
return Response.ok("{}", MediaType.APPLICATION_JSON).build();
}
return Response.ok(
new MultiPart()
.bodyPart("This is the first segment", MediaType.TEXT_PLAIN_TYPE)
.bodyPart("<outer><inner>value</inner></outer>", MediaType.TEXT_XML_TYPE),
MultiPartMediaTypes.MULTIPART_MIXED_TYPE
).build();
}
}
希望这有帮助。