I have a web application that works great
and I would like to integrate it with another web application that is rest interface in JSON implemented using Jersey
the spring controllers are using RequestMapping like:
#Controller
public class AdminPrinterController {
#RequestMapping(value = "/contact/view.action")
public #ResponseBody
Map<String, ? extends Object> view() throws Exception {...}
while the Jersey controllers look like?:
#Path("/printerList")
public class PrinterListApi{
#Path("/internalPrinterList/{locationId}")
I integrated the code, but its obviously not working... probably because spring is intercepting the Jersey URL
this is my spring filter mapping:
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Can Jersey live together with spring?
Or should i switch to Spring rest implementation
Thank you
It's possible. You'll have to include the spring-jersey dependency in your project. Then you should be able to use the standard spring servlet mapping. I pulled this out of a pretty old project so you might want to check for updated versions. Mixing jersey and spring is a little messy though. I think the more modern way is to follow a spring boot rest tutorial.
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.23</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.23</version>
<exclusions>
<exclusion>
<artifactId>jersey-bean-validation</artifactId>
<groupId>org.glassfish.jersey.ext</groupId>
</exclusion>
<exclusion>
<artifactId>bean-validator</artifactId>
<groupId>org.glassfish.hk2.external</groupId>
</exclusion>
</exclusions>
</dependency>
Sorry for the edit. I forgot in your web.xml you'll have to point to the jersey servlet container.
<servlet>
<servlet-name>SpringApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.application.MainApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Related
I have developed a Rest-full WS which runs successfully on tomcat.
The application deploys succesfully in Weblogic 12.2.1 without any exceptions. However, on hitting the URI, I face "HTTP method GET is not supported by this URL Rest-full WS" in response.
This happens for POST methods as well and while generating wadl also.
Below is the Rest WS implemation class
#POST
#Path("/activateService")
#Consumes({MediaType.APPLICATION_XML})
#Produces({MediaType.APPLICATION_XML})
public Response crunchifyREST**(JsonObject model**, #Context
HttpServletRequest request) {
}
#GET
#Path("/verify")
#Produces(MediaType.TEXT_PLAIN)
public Response verifyRESTService(InputStream incomingData) {
String result = "GMPPMediatorTIMService Successfully started..";
// return HTTP response 200 in case of success
return Response.status(200).entity(result).build();
}
web.xml :-
<servlet>
<servlet-name>FacadeHandsetTimpay</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.trivnet.mediator.tim.services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacadeHandsetTimpay</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
I am using URI as :- http://localhost:8070/FacadeHandsetTimpay/api/getLastTimPayTransactions
I checked below links , however I could find any help
HTTP method GET is not supported by this URL. (Java rest api with jersey)
I got the solution . 12.2.1, WebLogic Server Jersey 1.x server-side APIs are no longer supported. You should use the corresponding standard JAX-RS 2.0 or Jersey 2.x APIs instead. The Jersey 1.x client API is deprecated. It is recommended that you update your RESTful client applications to use the JAX-RS 2.0 client APIs at your earliest convenience.
So , remove these dependencies:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0-m12</version>
And use these dependencies:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.17</version>
</dependency>
And also change web.xml as follows:-
<servlet>
<servlet-name>FacadeHandsetTimpay</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.trivnet.mediator.tim.services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacadeHandsetTimpay</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
Source:-
https://docs.oracle.com/middleware/12212/wls/RESTF/intro-restful-service.htm#RESTF109
This is my web.xml
<servlet>
<servlet-name>Simulator HTTP API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Simulator HTTP API</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
and this is my simple web service:
#Path("partner")
public class PartnerAPI {
#Path("/mt")
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sendMT() {
return "Sent";
}
}
when i call it like this:
http://localhost:8080/myprojectname/partner/mt
i get 404 error mot found, what am i doing wrong?
Update
this is my maven
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.1</version>
</dependency>
You have different deployment options in Jersey 2:
If you want to do it via web.xml you have to add the an init-param where you specify which packages should be scanned:
<servlet>
<servlet-name>Simulator HTTP API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>insert.packagename.where.your.class.is.here</param-value>
</init-param>
</servlet>
Another option is to create a basic class to configure your REST application.
This would look like this:
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
#ApplicationPath("/test")
public class YourApplication extends ResourceConfig {
public YourApplication() {
this.packages("insert.packagename.where.your.class.is.here");
}
}
Make sure to update the string with the package name where your PartnerAPI class is.
Then add the value inside #ApplicationPath to your URL.
The link would look like this: http://localhost:8080/myprojectname/test/partner/mt
More information: Jersey docs: Chapter 4. Deploying a RESTful Web Service
i am trying to make Wadl file for rest services in my application , i am using resteasy , all tutorials and examples is to make it with maven & jersy
i dont use maven i use eclipse Wildfly and Resteasy , is there is any explanation how to make this .
thank you.
As of Resteasy 3.0.14-Final this should be possible:
https://issues.jboss.org/browse/RESTEASY-166
https://docs.jboss.org/resteasy/docs/3.1.0.Final/userguide/html/WADL.html
Add this to your web.xml:
<servlet>
<servlet-name>RESTEasy WADL</servlet-name>
<servlet-class>org.jboss.resteasy.wadl.ResteasyWadlServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RESTEasy WADL</servlet-name>
<url-pattern>/application.xml</url-pattern>
</servlet-mapping>
I tried this on WildFly 10.1.0 and I had to include an extra library:
Maven artifact: https://javalibs.com/artifact/org.jboss.resteasy/resteasy-wadl
Add the following to your pom.xml:
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-wadl -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-wadl</artifactId>
<version>3.0.14.Final</version>
<exclusions>
<exclusion>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
</exclusion>
</exclusions>
</dependency>
Next re-publish and access the WADL at [context-root]/application.xml
I'm trying to set up some REST services on Jetty using Jersey JAXRS. I can't get json data through to my REST service class though. My ajax requests keep getting the "Unsupported Media Type" error and status. I get this regardless of what #Produces and #Consumes annotations I add to my methods though they should both be MediaType.APPLICATION_JSON.
I can't find decent documentation on Jersey and the loads of questions, blogs, and other resources all seem to be out of date. Looks like Jersey has undergone a lot of changes recently and I'm at a loss as to where I should be looking. I set up the following based on the jersey webapp archetype:
web.xml:
<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>com.my.package.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Code snippet:
#Path("/users")
public class UserService {
// Plain text works!
#GET
#Consumes(MediaType.TEXT_PLAIN)
public String list(){
return "Got it!";
}
// JSON doesn't work! >:(
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public User create(User user) {
Mocks.USERS.add(user);
return user;
}
My parent pom manages these dependencies ahd the second of these two is a dependency in my jax-rs project pom.
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>2.2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.2</version>
</dependency>
Do I need something to add support for JSON?
Have you read a chapter dedicated to JSON in the Users Guide? The easiest way would be adding a dependency on MOXy and JSON support would work out-of-the-box (you don't need to explicitly register features the modules provides to make it work as opposed to other JSON modules in Jersey):
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.2</version>
</dependency>
Anyways Jersey provides more modules that would help you with handling JSON media type:
MOXy (examples: json-moxy, bean-validation-webapp)
Jackson (example: json-jackson)
Java API for JSON Processing (JSON-P) (example: json-processing-webapp)
Jettison (example: json-jettison)
Seems Drew was on the right track in his comment. But the answer (for Jersey 2.2 + Jackson at least) was a more up-to-date provider
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.2.3</version>
</dependency>
Using this required no configuration. Use this with the two dependencies in the original questions and you're in business.
JSON start working for me just with 2 dependencies:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.2.3</version>
</dependency>
This is the structure of my project (exactly these five files):
/p1
pom.xml
/src
/main
/java
/webapp
a.html
b.xhtml
/WEB-INF
faces-config.xml
web.xml
I'm deploying this WAR to GlassFish and I can successfully access this URL: http://localhost:8080/p1/a.html. When I'm trying to open http://localhost:8080/p1/b.xhtml I'm getting a message
The requested resource (/p1/b.xhtml) is not available.
What am I doing wrong?
ps. My dependencies from pom.xml:
...
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>${jsf.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>${jsf.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.facelets</groupId>
<artifactId>jsf-facelets</artifactId>
<version>${facelets.version}</version>
</dependency>
...
This is my web.xml (core part of it):
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
My faces-config.xml:
<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
</application>
For the Maven side, things looks ok, except that facelets should also be provided. Actually, I use the following dependency:
<!-- This dependency will bring in everything we need for JAVA EE6 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
For the JSF part, nothing in the server logs? Just in case, could you add the following to your web.xml to see if you get more useful output:
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
As a side note, you don't need your faces-config.xml as Facelets is the default view handler in JSF 2.0. But this shouldn't be a problem.
PS: Personally, I prefer to map the Faces Servlet on something like *.jsf (to clearly de-correlate any mapped url from the actual .xhtml facelet page that will be processed by the Faces Servlet).
See also
JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?