I'm trying to implement a REST Server API using Java-ee following this tutorial. Instead of Glassfish, I use Tomcat.
I could develop a servlet
#WebServlet(name = "hello", urlPatterns = "/")
public class HelloWorld extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("=)");
}
}
And join http://localhost:9080/ I can see the smiling face. But when I try to access to the api path (http://localhost:9080/api/recommend/all) I also get the face. If I remove the servlet class, I get a 404 error. I suppose I need something else to build automatically the api but I don't know what.
Could someone tell my what is missing? What should I do?
Update:
In Intellij's Java Enterprise View I see:
Web > HelloWorld
RESTful WS > recommend > all
These are my api classes:
#ApplicationPath("/api")
public class REST_Config extends Application {
}
And the specific method
#Path("recommend")
public class RecommenderController {
#Path("/all")
#GET
#Produces("application/json")
public JsonArray getAll(){
JsonArrayBuilder builder = Json.createArrayBuilder();
builder.add(Json.createObjectBuilder().add("1", "2.5"));
return builder.build();
}
}
And the 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TestREST</groupId>
<artifactId>TestREST</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
</dependencies>
<build>
<finalName>TestREST</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins>
</build>
</project>
"Instead of Glassfish, I use Tomcat."
Look at this
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
This is nothing more than basically a bunch of interfaces for the EE spec. There is no implementation. Java EE servers will have the implementation. Tomcat is not an EE server. The only part of the EE spec it will definitely implements is the Servlet Specification. You are trying to work with the JAX-RS spec, where Tomcat for sure by default does not have an implementation for. So you need to add that implementation.
The easiest IMO to get started with, is Jersey. You can simple add this dependency
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.17</version>
</dependency>
And it will get you up and running. Keep the Jersey User Guide handy. It will come in use.
Also I don't know what JsonArray is, but what will happen when you run this is you will get some error similar to "No MessageBodyWriter found for JsonArray and media type application/json". You need to provider. If you are going to use the Java EE JSONP API, then you should add the this provider
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
<version>2.17</version>
</dependency>
As you get to working alot with JSON, you wil find this API to be difficult to maintain. I'd recommend using Jackson. If you don't already know it, I'd suggest learning it. It offers simple POJO to JSON mapping. For Jackson, you can add this dependency
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.17</version>
</dependency>
Related
I am kind of frustrated. I am trying to develop a simple "hello world" macro for confluence. But all the tutorials are not really working anymore for the actual SDK6.
I tried this tutorial:
https://developer.atlassian.com/confdev/tutorials/macro-tutorials-for-confluence/creating-a-new-confluence-macro#CreatingaNewConfluenceMacro-Step1.Createthepluginprojectandtrimtheskeleton
But as you can see the article discussing, it is not working correctly anymore. I think some elements have be modified with SDK6 and the tutorials are not up to date anymore.
I ask at the confluence-forum for help but without any luck. There are several post around this issue without any solution.
The problem is, that the addon / plugin is visible in the system administration panel but I can not use the macro on a page and I can not see the macro in the macro browser.
Now it works - Update
This is what I did:
1) Download SDK
I downloaded sdk-installer-6.2.4.exe and installed it
2) Creating new plugin
I created a new plugin for confluence by typing in
atlas-create-confluence-plugin
with these following group- and artifact ids
groupid : com.example.plugins.tutorial.confluence
artifactid : tutorial-confluence-macro-demo
version : 1.0-SNAPSHOT
package : package com.example.plugins.tutorial.confluence
3) Creating eclipse project
Then I created the eclipse project by typing in
atlas-mvn eclipse:eclipse
4) Modify pom.xml
I modified the pom.xml just like ppasler explained in his answer. I also modified the companyname and the version in order to check in confluence, if the modification will have an effect. The pom looks like this:
<?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>com.example.plugins.tutorial.confluence</groupId>
<artifactId>tutorial-confluence-macro-demo</artifactId>
<version>4.4-SNAPSHOT</version>
<organization>
<name>Hauke Company</name>
<url>http://www.example.com/</url>
</organization>
<name>tutorial-confluence-macro-demo</name>
<description>This is the com.example.plugins.tutorial.confluence:tutorial-confluence-macro-demo plugin for Atlassian Confluence.</description>
<packaging>atlassian-plugin</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.atlassian.confluence</groupId>
<artifactId>confluence</artifactId>
<version>${confluence.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.atlassian.plugin</groupId>
<artifactId>atlassian-spring-scanner-annotation</artifactId>
<version>${atlassian.spring.scanner.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.atlassian.plugin</groupId>
<artifactId>atlassian-spring-scanner-runtime</artifactId>
<version>${atlassian.spring.scanner.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope>
</dependency>
<!-- WIRED TEST RUNNER DEPENDENCIES -->
<dependency>
<groupId>com.atlassian.plugins</groupId>
<artifactId>atlassian-plugins-osgi-testrunner</artifactId>
<version>${plugin.testrunner.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2-atlassian-1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-confluence-plugin</artifactId>
<version>${amps.version}</version>
<extensions>true</extensions>
<configuration>
<productVersion>${confluence.version}</productVersion>
<productDataVersion>${confluence.data.version}</productDataVersion>
<enableQuickReload>true</enableQuickReload>
<enableFastdev>false</enableFastdev>
<!-- See here for an explanation of default instructions: -->
<!-- https://developer.atlassian.com/docs/advanced-topics/configuration-of-instructions-in-atlassian-plugins -->
<instructions>
<Atlassian-Plugin-Key>${atlassian.plugin.key}</Atlassian-Plugin-Key>
<!-- Add package to export here -->
<Export-Package>
com.example.plugins.tutorial.confluence.api,
</Export-Package>
<!-- Add package import here -->
<Import-Package>
org.springframework.osgi.*;resolution:="optional",
org.eclipse.gemini.blueprint.*;resolution:="optional",
*
</Import-Package>
<!-- Ensure plugin is spring powered -->
<Spring-Context>*</Spring-Context>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>com.atlassian.plugin</groupId>
<artifactId>atlassian-spring-scanner-maven-plugin</artifactId>
<version>1.2.6</version>
<executions>
<execution>
<goals>
<goal>atlassian-spring-scanner</goal>
</goals>
<phase>process-classes</phase>
</execution>
</executions>
<configuration>
<scannedDependencies>
<dependency>
<groupId>com.atlassian.plugin</groupId>
<artifactId>atlassian-spring-scanner-external-jar</artifactId>
</dependency>
</scannedDependencies>
<verbose>false</verbose>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<confluence.version>5.9.7</confluence.version>
<confluence.data.version>5.9.7</confluence.data.version>
<amps.version>6.2.4</amps.version>
<plugin.testrunner.version>1.1.1</plugin.testrunner.version>
<atlassian.spring.scanner.version>1.2.6</atlassian.spring.scanner.version>
</properties>
<!--
<properties>
<confluence.version>5.9.7</confluence.version>
<confluence.data.version>5.9.7</confluence.data.version>
<amps.version>6.2.3</amps.version>
<plugin.testrunner.version>1.2.3</plugin.testrunner.version>
<atlassian.spring.scanner.version>1.2.6</atlassian.spring.scanner.version>
<atlassian.plugin.key>${project.groupId}.${project.artifactId}</atlassian.plugin.key>
</properties>
-->
</project>
5) Starting eclipse
I imported the project to Eclilpse
Version: Mars.1 Release (4.5.1)
Build id: 20150924-1200
Java JDK 1.8.0_60
6) ExampleMacro class creating
I created the class "ExampleMacro"
package com.example.plugins.tutorial.confluence;
import com.atlassian.confluence.content.render.xhtml.ConversionContext;
import com.atlassian.confluence.content.render.xhtml.XhtmlException;
import com.atlassian.confluence.macro.Macro;
import com.atlassian.confluence.macro.MacroExecutionException;
import com.atlassian.confluence.xhtml.api.MacroDefinition;
import com.atlassian.confluence.xhtml.api.MacroDefinitionHandler;
import com.atlassian.confluence.xhtml.api.XhtmlContent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class ExampleMacro implements Macro
{
private final XhtmlContent xhtmlUtils;
public ExampleMacro(XhtmlContent xhtmlUtils)
{
this.xhtmlUtils = xhtmlUtils;
}
#Override
public String execute(Map<String, String> parameters, String bodyContent, ConversionContext conversionContext) throws MacroExecutionException
{
String body = conversionContext.getEntity().getBodyAsString();
final List<MacroDefinition> macros = new ArrayList<MacroDefinition>();
try
{
xhtmlUtils.handleMacroDefinitions(body, conversionContext, new MacroDefinitionHandler()
{
#Override
public void handle(MacroDefinition macroDefinition)
{
macros.add(macroDefinition);
}
});
}
catch (XhtmlException e)
{
throw new MacroExecutionException(e);
}
StringBuilder builder = new StringBuilder();
builder.append("<p>");
if (!macros.isEmpty())
{
builder.append("<table width=\"50%\">");
builder.append("<tr><th>Macro Name</th><th>Has Body?</th></tr>");
for (MacroDefinition defn : macros)
{
builder.append("<tr>");
builder.append("<td>").append(defn.getName()).append("</td><td>").append(defn.hasBody()).append("</td>");
builder.append("</tr>");
}
builder.append("</table>");
}
else
{
builder.append("You've done built yourself a macro! Nice work.");
}
builder.append("</p>");
return builder.toString();
}
#Override
public BodyType getBodyType()
{
return BodyType.NONE;
}
#Override
public OutputType getOutputType()
{
return OutputType.BLOCK;
}
}
7) Modified the atlassian-plugin.xml file
<atlassian-plugin key="${atlassian.plugin.key}" name="${project.name}" plugins-version="2">
<plugin-info>
<description>${project.description}</description>
<version>${project.version}</version>
<vendor name="${project.organization.name}" url="${project.organization.url}" />
<param name="plugin-icon">images/pluginIcon.png</param>
<param name="plugin-logo">images/pluginLogo.png</param>
</plugin-info>
<!-- add our i18n resource -->
<resource type="i18n" name="i18n" location="tutorial-confluence-macro-demo"/>
<xhtml-macro name="tutorial-confluence-macro-demo" class="com.example.plugins.tutorial.confluence.ExampleMacro" key="my-macro">
<parameters/>
</xhtml-macro>
<!-- add our web resources -->
<web-resource key="tutorial-confluence-macro-demo-resources" name="tutorial-confluence-macro-demo Web Resources">
<dependency>com.atlassian.auiplugin:ajs</dependency>
<resource type="download" name="tutorial-confluence-macro-demo.css" location="/css/tutorial-confluence-macro-demo.css"/>
<resource type="download" name="tutorial-confluence-macro-demo.js" location="/js/tutorial-confluence-macro-demo.js"/>
<resource type="download" name="images/" location="/images"/>
<context>tutorial-confluence-macro-demo</context>
</web-resource>
</atlassian-plugin>
8) Starting confluence
atlas-clean
atlas-package
atlas-debug
9) Logged into confluence
Here the result of the confluence administration page
And now I can find it also in the macro browser and it works
Thanks
Hauke
working with atlassian plugins can be really frustrating :)
I checked out the macro source code from bitbucket and made the following changes in the pom
<properties>
<confluence.version>5.9.7</confluence.version>
<confluence.data.version>5.9.7</confluence.data.version>
<amps.version>6.2.4</amps.version>
<plugin.testrunner.version>1.1.1</plugin.testrunner.version>
</properties>
Then run
atlas-clean
atlas-package
atlas-debug
After that I was able to add the macro with the macro browser (with a confluence 5.8.6 instance).
Unfortunatly I had no time to check the differences between the source code and the tutorial, but my solution will give you a working state to try new stuff.
Your image is displaying ${atlassian.plugin.key}. Is your Macro add-on working properly. It is displayed in the macro browser but can you use it on the page? I also noticed you commented out atlassian.plugin.key in your pom.xml.
The use of <Atlassian-Plugin-Key> here tells the plugin system that you are a transformerless plugin and that it should skip the slow transformation step. This is VERY IMPORTANT. Without this entry in your Manifest, the plugin system will try to transform your plugin, and you will lose the load time speed benefits. You are also likely to see Spring-related errors. Do not forget to specify this entry.
See: Atlassian Spring Scanner
The new way of importing components is to use Atlassian Spring Scanner. It looks like your mixing the old and new way of importing components by commenting out atlassian.plugin.key.
Check out: Build a Macro Add-on
Confluence examples: Confluence Add-on Development examples
Am new to JavaEE and have some issues getting custom ContainerRequestFilter to run in Jersey. I read the jersey documentation some more and created a new clean project straight from the 'jersey-quickstart-webapp', added the filter seen below but no luck (added an empty beans.xml as well).
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
#Provider
#PreMatching
public class MyFilter implements ContainerRequestFilter {
#Override
public void filter(ContainerRequestContext requestContext) throws IOException {
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).entity("User cannot access the resource.").build());
}
}
Was uncertain if Prematching and Provider was complementary or not so i used both then each separately, but didnt work (MyReasource just served as without filter). Tried throwing an exception in MyFilter but that didnt run either.
So i searched through StackOverflow and found 'http://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/' which points to that you actually needs to implement registrations in Application or ResourceConfig class. I tried this (didnt work) but i atleast got a warning for the resource class now, 'No resource methods have been found for resource class a.b.MyFilter'
My Application class now looks like below (tried scan package but didnt make a difference. Without the manual filter registration i didnt get the warning either).
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import javax.ws.rs.ApplicationPath;
#ApplicationPath("resources")
public class RestApplication extends ResourceConfig {
public RestApplication() {
//packages("a.b");
register(MyFilter.class);
register(MyResource.class);
property(ServerProperties.TRACING, "ALL");
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
</web-app>
pom.xml
<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>a.b</groupId>
<artifactId>server</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>server</name>
<build>
<finalName>server</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</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>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.22.1</jersey.version>
<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
All files lies in the 'a.b' (i.e my package) root. Any ideas on how to get the filter actually running and i would be very greatful ;). I presume it shouldnt be this hard to get this working so i guess im missing something here?
Let me walk you through what's going on. When you first created the jersey-quickstart-webapp archetype, it gave you this
<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>a.b</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
What this init-param jersey.config.server.provider.packages does is tell Jersey what package(s) to scan for resource classes annotated with #Path, and provider classes annotated with #Provider.
So from that point, all you needed to do was add the #Provider to the filter, and it would have worked.
But then you decided to clear out the web.xml and use the ResourceConfig with the #ApplicationPath. For this to work Jersey takes advantage of the Servlet 3.0 pluggability mechanism, as mentioned in this answer. For that to work we need to make sure we have the jar that has the JerseyServletContainerInitializer. That's where we need to look at the pom.xml file
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
The comment is telling you that if you don't need Servlet 2.5 support, you should use jersey-container-servlet instead of jersey-container-servlet-core. I don't know, it might be poorly worded. Maybe instead it should say if you want Servlet 3.x support, change it. But in any case, the jersey-container-servlet has the JerseyContainerServletInitializer that we need. So if you want to go web.xml-less, then just switch out the dependency.
Im trying to implement Json support in a JavaEE project but had issues with MOXy related exceptions being generated. I read on jersey.java.net that MOXy should be autodiscoverable but it doesnt seem to work when i try.
So to make this easy to pinpoint i just generated a new 'jersey-quickstart-webapp' project and changed MyResource as below (my goal is to use an Application class instead of web.xml but this was the simplest way och pinpointing it. The error occurs no matter what).
#Path("myresource")
public class MyResource {
#GET
#Produces(MediaType.APPLICATION_JSON)
public Response getIt() {
return Response.status(Response.Status.ACCEPTED).entity(new TestEntity()).build();
}
}
TestEntity class (in same package):
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class TestEntity {
private String content = "SOME CONTENT";
public String getContent() {
return content;
}
}
POM.xml:
<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>a.b.c</groupId>
<artifactId>server</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>server</name>
<build>
<finalName>server</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</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>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.22.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
I deployed this on a clean Glassfish 4.1.1 using IntelliJ.
After this i receive
java.lang.ClassNotFoundException:
javax.xml.parsers.ParserConfigurationException not found by
org.eclipse.persistence.moxy
So i add beans.xml as below (tried empty as well as i saw indicated in Oracle docs)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>
And i get this error when deploying
java.lang.NoClassDefFoundError: Could not initialize class
org.eclipse.persistence.jaxb.BeanValidationHelper
I tried , for fun, removing the web.xml, changing the dependency jersey-container-servlet-core to jersey-container-servlet and creating an Application class instead (as discussed in ContainerRequestFilter wont run in JavaEE jersey project) but gives the same error. Infact it gives the same error if publishing a clean javaee-api 7.0 dependant project instead of jersey dependencies and gave same error (i suppose glassfish using jersey anyway).
So i guess im missing something here, any kind soul that could fill me in on what? :)
Downgraded to Glassfish 4.1.0 and then it worked perfectly. Some issue perhaps with the 4.1.1 release? Will try the nightly as well but it works now.
I managed to get past this issue by updating the Manifest in the org.eclipse.persistence.moxy.jar file that comes with Glassfish 4.1.1, rather than downgrading to Glassfish 4.1.0
Steps I took:
Get the updated Manifest.mf file from this post (attached on 2015-03-26 06:08:50 EDT)
https://bugs.eclipse.org/bugs/show_bug.cgi?id=463169
Replace the Manifest.mf file in the org.eclipse.persistence.moxy.jar file with the one you downloaded.
The file is found here:
{c}:\glassfish4\glassfish\modules\org.eclipse.persistence.moxy.jar
Restart Glassfish
Thanks to those who posted and fixed this issue on bugs.eclipse.org
Instead of downgrading to 4.1.0, I found a switch to Payara a good bet.
Is there any documentation available related to deploying eureka server on web container like tomcat. I use the spring provided sample and created a war, also renamed it to 'eureka.war' but the dashboard is not displayed..
The code works fine with spring boot but looks some configuration is required for deploying it as war.
See this commit: https://github.com/spring-cloud-samples/eureka/commit/1de7c89cf3f79e4707dbabe91ea60eb06f2268aa
In pom.xml
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
In EurekaApplication.java
public class EurekaApplication extends SpringBootServletInitializer { /*...*/ }
I am developing a straightforward maven-based JavaEE application in IntelliJ IDEA, and obviously I would like to use Wildfly 8 for both development and production. I simply need to expose some entities through some RESTful web services. Those entities have bidirectional relationships, which leads to a loop when they are going to be serialized into JSON.
Newer versions of Jackson are able to handle this kind of situation with a special annotation. To get that to work, I need to exclude Wildfly's built-in JSON serializer / jackson provider / whatever it is and use the newer version that comes bundled with my application. I have followed the instructions I have found on the web and came up with this jboss-deployment-structure.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.codehaus.jackson.jackson-jaxrs" />
<module name="org.codehaus.jackson.jackson-core-asl" />
<module name="org.codehaus.jackson.jackson-mapper-asl" />
<module name="org.codehaus.jackson.jackson-xc" />
</exclusions>
</deployment>
</jboss-deployment-structure>
The problem is, it doesn't work. Even when I set my pom.xml to something like this:
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.3.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.0.6.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
which clearly indicates that nothing should come bundled with my application, I still get this StackOverflowError (caused by the infinite loop) which roots in org.codehaus.jackson package. This is turn means that built-in version of Jackson is still in the works and is not excluded. How can I fix this?
Thanks in advance.
UPDATES
I changed the title because I guess the problem is even bigger. Either I am doing something terribly wrong or there is a serious problem with Wildfly.
I have created a pom.xml with all the jackson 2 libraries (com.fasterxml.jackson*) with a "compile" scope. The libraries are included in the WEB-INF/lib folder. I wrote a #Provider according to Jackson JAX-RS FAQ and I can verify that it is actually read by the JAX-RS implementation (RestEasy), simply because the deployment fails if I do not include jackson libraries in WEB-INF/lib with a ClassNotFoundException. However, I am still getting infinite recursion errors with org.codehaus.X (Jackson 1).
I don't care how, I just need a solution to fix this infinite recursion in Wildfly.
The solution is to create a class which implement MessageBodyWriter<Object> using Jackson's ObjectMapper:
#Provider
#Produces("application/json")
public class JacksonMapper implements MessageBodyWriter<Object> {
#Override
public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
return true;
}
#Override
public long getSize(Object object, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
return 0;
}
#Override
public void writeTo(Object object, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> stringObjectMultivaluedMap, OutputStream outputStream)
throws IOException, WebApplicationException {
outputStream.write(new ObjectMapper().writeValueAsBytes(object));
}
}
There's no need for any exclusion or Wildfly-specific descriptors.
It doesn't matter if you include the dependencies or not (compile or provided scopes both work fine) as Jackson 2 is included in Wildfly. However, for an unknown reason, it is near to impossible to deactivate Jackson 1.
This solution brings Jackson 2 into the works. Now you can easily avoid serialization loops using the #JsonIdentityInfo annotation. More info here.