有几天我试图让Glassfish 4.1.1和Jersey一起工作。棘手的部分是通过JSON进行通信时。
我尝试了很多解决方案来实现JSON数据传输。唉,还是不行。
Glassfish和Jersey是'标准',所以我猜有一种标准方法可以将这两者结合起来?配置JSON的正确方法是什么?
每当我开始使用JSON时,我都会遇到各种错误。 我得到的最后一个错误:
2016-11-25T15:59:52.070+0100|Warning: StandardWrapperValve[Jersey Web Application]: Servlet.service() for servlet Jersey Web Application threw exception
java.lang.ClassNotFoundException: javax.xml.parsers.ParserConfigurationException not found by org.eclipse.persistence.moxy [228]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
etc, etc.
maven依赖项是:
http://maven.apache.org/maven-v4_0_0.xsd“>
<modelVersion>4.0.0</modelVersion>
<groupId>nl.xyz</groupId>
<artifactId>GlassfishJerseyJson</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>GlassfishJerseyJson</name>
<build>
<finalName>GlassfishJerseyJson</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!-- <webXml>src\main\webapp\WEB-INF\web.xml</webXml> -->
</configuration>
</plugin>
<plugin>
<groupId>org.glassfish.maven.plugin</groupId>
<artifactId>maven-glassfish-plugin</artifactId>
<version>2.1</version>
<configuration>
<glassfishDirectory>${local.glassfish.home}</glassfishDirectory>
<user>admin</user>
<passwordFile>${local.glassfish.passfile}</passwordFile>
<domain>
<name>domain1</name>
<httpPort>8080</httpPort>
<adminPort>4848</adminPort>
</domain>
<components>
<component>
<name>${project.artifactId}</name>
<artifact>target/${project.build.finalName}.war</artifact>
</component>
</components>
<debug>true</debug>
<terse>false</terse>
<echo>true</echo>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.6.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.5.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.24.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
JQuery调用是:
function findResource() {
$.ajax({
type: 'GET',
accepts: { json: "application/json, text/javascript" },
dataType: "json",
url: resourceURL,
success: function(data){
alert( "Back from resource: name=" + data.name);
},
error: function(jqXHR, textStatus, errorThrown){
alert('Back from resource error: ' + textStatus + ' - Error: ' + errorThrown + " - Response: " + jqXHR.responseText);
}
});
}
资源java文件:
@Path("myresource")
public class MyResource {
@GET
@Produces( MediaType.APPLICATION_JSON)
public Wine2 getIt() {
Wine2 w = new Wine2();
w.setId( 100);
w.setCountry( "france");
return w;
}
}
配置:选项1:web.xml(保留默认的第一行):
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.coenraets.cellar</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
配置:选项2:MyApplication.java
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add( MyResource.class);
s.add( WineResource.class);
return s;
}
}
答案 0 :(得分:2)
只需使用以下代替你的moxy依赖:
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.6.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.5.1</version>
<scope>provided</scope>
</dependency>
libs包含在标准的Glassfish 4安装中,因此提供的范围就足够了。
你应该删除这些依赖项:
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
和
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<!-- <version>2.4.1</version>
<scope>provided</scope> -->
</dependency>
您不需要它们,并且可以将大多数泽西岛依赖项设置为提供。
如果你不能让它工作:我用示例项目创建了a gist,它只有4个文件:pom.xml,web.xml,MyResource.class和Cow。类。这包括基本设置所需的一切,它可以在没有任何其他库的Glassfish 4上运行。
答案 1 :(得分:2)
Yesssss !!!!我找到了完整的答案。
解决方案是使用原始(见上文)maven libs ANd将GLASSFISH / modules文件夹中的以下文件更改为版本2.5.0(或类似版本):
解决方案来自[此网页] [1]和类似网站。
在这篇文章和其他文章中,$ Glassfish / modules中的org.eclipse.persistence.moxy.jar文件被替换为2.5.0版本。
答案 2 :(得分:0)
尝试添加库:Java EE7 API库,JAX-RS,Jersey。
答案 3 :(得分:0)
您的解决方案可以是为Jersey应用程序提供自定义JSON提供程序。它的定义如下:
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Produces(MediaType.APPLICATION_JSON)
public class CustomJsonProvider extends JacksonJaxbJsonProvider {
private static final ObjectMapper MAPPER = new ObjectMapper();
static {
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
MAPPER.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.DEFAULT);
MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
}
public CustomJsonProvider() {
super();
setMapper(MAPPER);
}
}
然后是一个将自定义JSON提供程序注册到Jersey应用程序上下文的功能:
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
public class MarshallingFeature implements Feature {
@Override
public boolean configure(FeatureContext context) {
context.register(CustomJsonProvider.class, MessageBodyReader.class, MessageBodyWriter.class);
return true;
}
}
最后,在Jersey Application类构造函数中注册该功能:
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("ws")
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
register(MarshallingFeature.class);
// register ressource classes
}
}
这些是您需要添加到Maven pom.xml的依赖项:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.8.6</version>
<type>jar</type>
</dependency>
希望它有所帮助。