RESTEasy Could not find MessageBodyWriter - Jackson Configured - java

I recently set up RESTEasy on Tomcat 7. We do not use Maven. I mention this early on, because all of the answers I've found to my question to date suggest adding a reference to the pom.xml. Adding references to my project, so far, hasn't solved my problem (but, maybe I'm missing something).
At this point, I'm just trying to recreate the out-of-the-box examples I've seen online where I should be able to correctly annotate a POJO, and return it as JSON using RESTEasy, assuming I have my build configured correctly. My build references:
- jackson.core.asl
- jackson.jaxrs
- jackson.mapper.asl
and all the required RESTEasy jars. In fact, I can successfully obtain the JSON I want from a GET if I return a Response (as in the example I found here: RestEasy: Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: application/json). Of course, that's not using Jackson through RESTEasy, at all.
Whenever I try to return an object, however, of any kind, I get:
Could not find MessageBodyWriter for response object of type: ... application/json
I'm not sure what I'm doing wrong, as all the answers I can find suggest that what I need to do is make sure that jackson is correctly referenced---which, from all I can tell, it is. Below is the relevant portion of my web.xml.
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>rest_services.RootRestService</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/services</param-value>
</context-param>
Here is my Service method:
#GET
#Path("/getTables")
#Produces(MediaType.APPLICATION_JSON)
public List<MyTable> getTables() {
...
}
Here is the class it's returning:
#XmlRootElement
public final class MyTable {
...
}
EDIT :
Here are the classes I added to my classpath (and a few others)
:
Any help is greatly appreciated.

I was never able to get Jackson working, but I switched to Jettison, and all works fine. This is good enough for me, for now.

Related

ContainerResponseFilter not executed

I want to build a REST-API with Jax-Rs with cors enabled. So I googled how to do and found this:
http://www.developerscrappad.com/1781/java/java-ee/rest-jax-rs/java-ee-7-jax-rs-2-0-cors-on-rest-how-to-make-rest-apis-accessible-from-a-different-domain/
The solution is a ResponseFilter, that adds some header information to every response, so that the browser of the user knows, that cross domain accesses are allowed.
Because the ResponseFilter is not executed when I do any request (tried GET, POST and OPTIONS), I googled again and found this:
ContainerResponseFilter not working
#lefloh gave a good answer, that sounds logic (to remove the annotation #PreMatching). It did so, but nevertheless my filter is not invoked, when I do a Http-Request.
This is my code:
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
#Provider
public class RESTServiceResponseFilter implements ContainerResponseFilter {
#Override
public void filter(ContainerRequestContext parContainerRequestContext, ContainerResponseContext parContainerResponseContext) throws IOException {
parContainerResponseContext.getHeaders().add( "Access-Control-Allow-Origin", "*" );
parContainerResponseContext.getHeaders().add( "Access-Control-Allow-Credentials", "true" );
parContainerResponseContext.getHeaders().add( "Access-Control-Allow-Methods", "GET, POST, DELETE, PUT" );
parContainerResponseContext.getHeaders().add( "Access-Control-Allow-Headers", "Content-Type" );
}
}
I kept on googling and found out, that I forgot to add the Filter to the web.xml. So I also did that:
<servlet>
<display-name>webinterface.api</display-name>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>de.tsystems.lbus.apprestserver</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>de.apprestserver.filter.RESTServiceResponseFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<url-pattern>/TNR/*</url-pattern>
</servlet-mapping>
I have no more ideas now and want to ask you, if you maybe have a solution for me. Thanks in advance!
You're using Jersey 1 (can tell by the com.sun.jersey in your web.xml). The filter implementation you are using (or showing us) is Jersey 2. There's subtle difference, but it's a major difference. The latter will not work with Jersey 1.
And the fact that it even compiles (if that's the case) means that you need to get rid of some dependencies. The class you have is a JAX-RS 2 class (interface). Any JAX-RS/Jersey 2 dependency, you might have, get rid of them. They don't play well (maybe not cause of issue, but get rid of them to drop any confusion)
Jersey 1 == com.sun.jersey (keep)
Jersey 2 == org.glassfish.jersey (get rid of)
JAX-RS 2 api == javax.ws.rs-api (get rid of)
See here for Jersey 1 implementation and configuration

Spring 4.1.4 and Jackson 2.5, annotation driven setup not converting REST responses to JSON

I coming back to Spring after spending 5 years doing other things. I have an initial projects which is designed to provide a HTTP REST service that returns JSON.
My problem is that I cannot get the service to convert the response to JSON. Instead I get errors like this:
javax.servlet.ServletException: Circular view path [hello]: would dispatch back to the current handler URL [/hello] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:205) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:145) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
...
My web.xml looks like this:
<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">
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>au.com.abc.service</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>fxServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>au.com.abc.controller</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>fxServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
And my Controller class looks like this:
#RestController
public class FXRESTController {
#RequestMapping(value = "/hello")
public Map<String,Object> rootContextHandler() {
Map<String,Object> data = new HashMap<>();
data.put("X", "abc");
return data;
}
}
It really cannot get any simpler. I've been passing the request header Accept='application/json', but it's still not working. I have had this type of thing working in the past, but I don't have that code anymore. I can also see this in the logs:
... Invoking request handler method: public java.util.Map au.com.abc.controller.FXRESTController.rootContextHandler()
... Service responding
... Invoking afterPropertiesSet() on bean with name 'hello'
... Rendering view [org.springframework.web.servlet.view.JstlView: name 'hello'; URL [hello]] in DispatcherServlet with name 'fxServlet'
... Added model object 'X' of type [java.lang.String] to request in view with name 'hello'
... Error rendering view [org.springframework.web.servlet.view.JstlView: name 'hello'; URL [hello]] in DispatcherServlet with name 'fxServlet'
Which suggests to me that it's trying to render a JSTL view. Why - I don't know considering I've asked for JSON.
Any ideas what I've done wrong?
I've read a ton of blogs and so far I cannot see any differences between what they have done and what I've done.
Oh and here are my gradle resolved dependencies:
You only have a controller, that doesn't do anything for enabling JSON.
You have to have a #Configuration annotated class which is also annotated with #EnableWebMvc to have automatic JSON conversion enabled. See also this section of the reference guide.
#Configuration
#EnableWebMvc
public class WebConfiguration {}
You are running with the DispatcherServlet defaults which are very basic.
Use #ResponseBody.
ALso donot direct all request to spring
<servlet-mapping>
<servlet-name>fxServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
ALways better to add a layer like this:
<servlet-mapping>
<servlet-name>fxServlet</servlet-name>
<url-pattern>/webapp/*</url-pattern>
</servlet-mapping>
FOr a given project only following request will be directed to spring
http://localhost:8080/Proj/app/hello
Other request can be handled different..thus you can handle request in a different way and not dependent on spring..like
http://localhost:8080/Proj/servlethandler
The above request will not go to spring and can be intercepted by a servlet etc..

Multiple JAX-RS applications in the same WAR

I have
#ApplicationPath("/resourcesP")
public class RestfulPrediction extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> set = new HashSet<Class<?>>();
set.add(PredictionsRS.class);
return set;
}
}
And
#ApplicationPath("/resourcesA")
public class RestfulAdage extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> set = new HashSet<Class<?>>();
set.add(Adages.class);
return set;
}
}
Two different ApplicationPath and the class are as follows.
#Path("/")
public class service.Adages {}
#Path("/")
public class webservices.PredictionsRS {}
Both of them are declared in different ApplicationPath. I'm using Jersey and the config in web.xml looks like
<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>
service
webservices
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
And I'm getting
SEVERE: Conflicting URI templates. The URI template / for root
resource class service.Adages and the URI template / transform to the
same regular expression (/.*)?
Why if I have two different ApplicationPath this exception comes at startup ?
If I take out a package in param-value this works, also if I change one of the #Path annotations this works, so it is a problem with my configuration ?
I'm using Jersey 1.10. Thanks all.
You did not define your JAX-RS applications in your web.xml. Try the following:
<servlet>
<servlet-name>full.name.RestfulAdage</servlet-name>
</servlet>
<servlet>
<servlet-name>full.name.RestfulPrediction</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>full.name.RestfulPrediction</servlet-name>
<url-pattern>/resourcesP/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>full.name.RestfulPrediction</servlet-name>
<url-pattern>/resourcesA/*</url-pattern>
</servlet-mapping>
and remove the #ApplicationPAth annotations from code.
I checked the above code with Jersey 2.7, servlet container 3.0 and it works. If still having that bug, try upgrading to Jersey 1.17 (which should not change any behavior from Jersey 1.10, and fix bugs instead) and eventually using also a servlet container 3.0.
UPDATE
After checking the possibilities the configuration below work with Jersey 1.17
<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>
com.koitoer.webservices
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
It seems there is bug in the spec in older version of Jersey that kind of circle back the references and mark as duplicate endpoints. Using the configuration above both endpoints load without any problem.
8/04/2014 09:13:40 PM
com.sun.jersey.server.impl.container.servlet.JerseyServletContainerInitializer
addServletWithApplication INFO: Registering the Jersey servlet
application, named com.koitoer.webservices.chapter2.service2.RestfulPrediction, at the
servlet mapping, /resourcesP/*, with the Application class of the same
name
8/04/2014 09:13:40 PM com.sun.jersey.server.impl.container.servlet.JerseyServletContainerInitializer
addServletWithApplication INFO: Registering the Jersey servlet
application, named com.koitoer.webservices.chapter2.RestfulAdage, at
the servlet mapping, /resourcesA/*, with the Application class of the
same name
You should have a single subclass of javax.ws.rs.core.Application in your webapp, and then use different #Path annotation values on your service.Adages and webservices.PredictionsRS resource types.
AFAIK, in JEE6 containers, you are not allowed to have 2 such subclasses...

How to initialize Java EE 5 JAX-WS 2.0 Web Service with Parameters

Application configuration:
Web application using java first method of creating JAX-WS 2.0 Web Services with annotations.
WebLogic 10.3
My Requirements
The requirements I have are to deploy a single web service implementation class, but change logic based on the URL from which the service was accessed.
Question:
I'm assuming a good way to do this is to deploy different mappings in web.xml and initialize them with different parameters. Is there a better way?
What is the best way to switch logic off the URL from which the web service was accessed? Should I try to configure two servlet mappings in web.xml with initialization parameters (tried, but couldn't get it to work), or should I parse the URL in the service impl? Any other alternatives?
What I've Tried (but didn't work)
I have tried adding the <init-param> in the <servlet> element in web.xml. However, can't get to the ServletConfig object inside the web service to retrieve the param. The web service does not have all the functionality of a standard Servlet (even if I implement Servlet or ServletContextListener). I only have access to the WebServiceContext (it seems) and from there I can only get <context-param> elements--but I would need <init-param> elements instead.
In web.xml, I enter two <servlet> elements using the same Java class, but which map to two different URLs as follows. Notice how the "source" param is different in each Servlet mapping.
<servlet>
<servlet-name>Foo</servlet-name>
<servlet-class>com.Foo</servlet-class>
<init-param>
<param-name>source</param-name>
<param-value>1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Foo</servlet-name>
<url-pattern>/Foo</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Bar</servlet-name>
<servlet-class>com.Foo</servlet-class>
<init-param>
<param-name>source</param-name>
<param-value>2</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Bar</servlet-name>
<url-pattern>/Bar</url-pattern>
</servlet-mapping>
You very well may have, but did you try using MessageContext at runtime to determine what the source is?
#WebService
public class CalculatorService implements Calculator
{
#Resource
private WebServiceContext context;
#WebMethod
public void getCounter()
{
MessageContext mc = wsContext.getMessageContext();
// you can grab the HttpSession
HttpSession session = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
// ...or maybe the path info is enough
String path = mc.get(MessageContext.PATH_INFO);
// the query itself should almost definitely be enough
String query = (String) mc.get(MessageContext.QUERY_STRING);
}
}
I got the idea from http://sirinsevinc.wordpress.com/category/jaxws/. Haven't tried it, though.

Calling a default servlet first in Java web application [duplicate]

This question already has answers here:
Change default homepage in root path to servlet with doGet
(2 answers)
Closed 7 years ago.
I want my SampleServlet to be called first whenever my java web application is accessed in this manner :
http://server:8080/appname/
Is there any way to implement this?
Use a Servlet filter to call your Servlet.
If you want to make a servlet your homepage then this worked for me on http://feelitlive.com/
<welcome-file-list>
<welcome-file>homepage</welcome-file>
</welcome-file-list>
...
<servlet>
<description>Shows stuff on the homepage</description>
<display-name>Homepage Servlet</display-name>
<servlet-name>HomepageServlet</servlet-name>
<servlet-class>com.cantorva.gigcalendar.servlets.HomepageServlet</servlet-class>
</servlet>
...
<servlet-mapping>
<servlet-name>HomepageServlet</servlet-name>
<url-pattern>/homepage</url-pattern>
</servlet-mapping>
That means that that users arriving at your application via the URL you specified will be welcomed by your servlet. It also creates an alias for the homepage at "/homepage" but you don't have to use that.
If you want to run some code on start-up then asalamon74's answer looks right.
Not sure what you mean but you need to map your servlet to "/"
<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Not sure what is your aim, but web application initialization can be achieved by ServletContextListener:
public class AppListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
// place your code here
}
public void contextDestroyed(ServletContextEvent event) {
}
}
and later in web.xml:
<web-app>
<listener>
<listener-class>
package.AppListener
</listener-class>
</listener>
...
</web-app>
If you want to run code on start-up indeed asalamon74's answer should be fine. If you have a legacy situation and you must use a servlet, the parameter load-on-startup can do the trick for you:
<servlet>
<servlet-name>SampleServlet</servlet-name>
<display-name>SampleServlet</display-name>
<description>Sample Servlet</description>
<servlet-class>...</servlet-class>
<init-param>...</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
The load-on-startup tag specifies that the servlet should be loaded automatically when the web application is started; the number value just gives a loading order to those loading on startup. If no value is specified, the servlet will be loaded when the container decides it needs to be loaded - typically on it's first access.

Categories