Building a REST web service - web-services

I am trying to build a simple Web service.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>REST_service</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Jersey</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
This is my class:
package com.service.user;
import javax.ws.rs.*;
#Path("/user/service")
public class UserService
{
#PUT
#Path("/create")
public void createUser(){
}
#GET
public void getUser(){
System.out.println("Inside GET");
}
}
I am running it using this request: http://localhost:8080/REST_service/
And it starts fine. But when I go to call the GET request, it doesnt get called.
http://localhost:8080/REST_service/rest/user/service
I am very confused as to what's going wrong and not able to debug.
Any help appreciated!

You still need to tell jersey to scan your packages for your resource classes so it can register them.
<servlet>
<servlet-name>Jersey</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.service
</param-value>
</init-param>
<servlet>

Related

Spring MVC app with SOAP web service using WSSpringServlet

I have created a simple Spring MVC web application and trying to expose the services as SOAP based JAX-WS services using JAX-WS commons RI implementation.
After deploying my application on Tomcat 7, when I try accessing my web service, I get a message as 404 Not Found: Invalid Request. Below are my configurations, kindly help in resolving this.
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">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes SOAP Web Service requests -->
<servlet>
<servlet-name>jaxws-servlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jaxws-servlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
WEB-INF/spring/appServlet/servlet-context.xml
<beans:bean id="customerService" class="com.home.service.CustomerService" />
<!-- Web Service definition -->
<beans:bean id="customerWS" class="com.home.ws.CustomerWS">
<beans:property name="customerService" ref="customerService" />
</beans:bean>
<wss:binding url="/ws/CustomerServ">
<wss:service>
<ws:service bean="#customerWS" />
</wss:service>
</wss:binding>
CustomerWS.java
#WebService
#SOAPBinding(style = Style.DOCUMENT, use = Use.LITERAL)
public class CustomerWS {
private CustomerService customerService;
#WebMethod
public Customer read(long id) {
return customerService.read(id);
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
}
CustomerService.java
#Service
public class CustomerService {
public Customer read(long id) {
Customer cust = null;
System.out.println("CustomerService.read invoked");
return cust;
}
}
pom.xml - included the dependency of jaxws-spring
<dependency>
<groupId>org.jvnet.jax-ws-commons.spring</groupId>
<artifactId>jaxws-spring</artifactId>
<version>1.9</version>
</dependency>
There are no errors while building or deploying the application. When I access the URL, still I see no errors in the server log files. However, the browser displays the message - 404 Not Found: Invalid Request
URL I am trying is - http://localhost:8080/crrs/ws/CustomerServ?wsdl
If I access my HomeController, it works fine. Home page is loaded as expected.
Appreciate any help. Thanks in advance.
I'm trying to do the same thing. My code is almost the same, I'm just using #Name and #Inject instead of #Service.
Just added extends SpringBeanAutowiringSupport to the #WebService class and it's working
The servlet mappings in the web.xml seem to be the cause.
<servlet-mapping>
<servlet-name>jaxws-servlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
[...]
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The spring DispatcherServlet named appServlet takes care of all urls after http://localhost:8080/crrs, even http://localhost:8080/crrs/ws/CustomerServ?wsdl.
The WSSpringServlet url-pattern cannot be reached.

Deploying java web application with javadoc as welcome-file

I have created java web application. When i deploy my applicaton to server using tomcat7, i want to deploy my javadoc as welcome-file so when i go to root directory of app i want to see reference. But instead tomcate gives lots of 404 error. Here is my project structure;
And my web.xml is;
<?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">
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<description>Multiple packages, separated by semicolon(;), can be specified in param-value</description>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.elhan.usermanager</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>javadoc/index.html</welcome-file>
</welcome-file-list>
</web-app>
How can i fix this?
nevermind. I solved the problem by moving javadoc contents to "Web Pages" directory. And in the web.xml file i changed javadoc/index.html with index.htmland deployed it. It works.

My Rest service won't work in Tomcat and I can't get it to send my resource, what am I doing wrong?

So I started to read this tutorial about how to develop a Restful service with Jersey. I want to develop Rest service that sends the data from a MySQL database to an Android client. I read and followed the steps on the tutorial and made my own resource classes, but when I tried to run the service on Apache I got the following error here.
I'm just starting to experiment with web services and Rest, I have read the information related to the subject from that IBM site and I thought I got the hang of it, but I'm really lost as to why is not working.
My web.xml is as follows
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TesterRest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey REST Service</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.mx.ipn.escom.testerRest.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
This is my resource class:
package com.mx.ipn.escom.testerRest.resources;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
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.Request;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.annotation.XmlRootElement;
import com.mx.ipn.escom.testerRest.dao.TemaDao;
import com.mx.ipn.escom.testerRest.db.Connector;
import com.mx.ipn.escom.testerRest.modelo.Tema;
#XmlRootElement
#Path("/temas")
public class TemaResource {
#Context
UriInfo uriInfo;
#Context
Request request;
#GET
#Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public List<Tema> getTemas() throws SQLException{
TemaDao temaDao = new TemaDao();
List<Tema> temas=temaDao.getTemas();
temaDao.terminarSesion();
return temas;
}
}
My class for database connection works fine, so that one isn't the problem.
I'm using Eclipse 3.6 to develop and Apache Tomcat 6.
I'm completely new to JAXB, so if anyone can give me guidelines to what kind of annotations am I missing I would appreciate it.
Based on your screenshot I think you should update your web.xml to have the correct package name:
<servlet>
<servlet-name>Jersey REST Service</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.mx.ipn.escom.testerRest.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
The mx is missing from your configuration.

Is it a REST web service?

I am working on a legacy application which have a .war with following structure (and I want to add my servlet in this war)
myApp.war
- axis2-web (Downloaded from http://ws.apache.org/axis2/download/1_1/download.cgi)
- META-INF
-- MANIFEST.MF
- WEB-INF
-- classes (But it don't have any `.class` file , it have `log4j.properties` file)
-- conf (Contains `axis2.xml`)
-- lib (contains many jars)
-- modules
-- services (Some `.aar` files)
-- web.xml
Here is the web.xml file
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Apache-Axis2</display-name>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>Apache-Axis Servlet</display-name>
<servlet-class>
org.apache.axis2.transport.http.AxisServlet</servlet-class>
<!--<init-param>-->
<!--<param-name>axis2.xml.path</param-name>-->
<!--<param-value>/WEB-INF/conf/axis2.xml</param-value>-->
<!--<param-name>axis2.xml.url</param-name>-->
<!--<param-value>http://localhot/myrepo/axis2.xml</param-value>-->
<!--<param-name>axis2.repository.path</param-name>-->
<!--<param-value>/WEB-INF</param-value>-->
<!--<param-name>axis2.repository.url</param-name>-->
<!--<param-value>http://localhot/myrepo</param-value>-->
<!--</init-param>-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>AxisRESTServlet</servlet-name>
<display-name>Apache-Axis Servlet (REST)</display-name>
<servlet-class>
org.apache.axis2.transport.http.AxisRESTServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>AxisAdminServlet</servlet-name>
<display-name>Apache-Axis AxisAdmin Servlet (REST)</display-name>
<servlet-class>
org.apache.axis2.transport.http.AxisAdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AxisRESTServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisAdminServlet</servlet-name>
<url-pattern>/axis2-admin/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>/axis2-web/index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/axis2-web/Error/error404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/axis2-web/Error/error500.jsp</location>
</error-page>
</web-app>
Is it a REST web service ? I Googled and found some tutorials on REST which says REST services have services.xml file but I couldn't find it in my application. I want to integrate my servlet in above war so what will be the right way to do it ?
PS: Please let me know if I am missing anything.
As I understand RESTFUL WS approach that the it has a traditional http request and response with get ,post ,put or delete operations directly passed to the web service and so the response doesn't have any special format (JSON-RPC,SOAP) .
For example , if we write simple code some-service.jsp and put this code on our server
the restful tells us to call the service using any http client passing it the required params
and then we will get the service response .
Now if your application meets this , it would be RESTful .
Note : there are no any specific implementation techniques for REST it is just a WS approach
hope this would help you

Serving static content with jetty 7, using defaultservlet configured from web.xml

This is jetty 7 and xml configured, not embedded.
I'm trying to serve a static file, crossdomain.xml, to an app that connects to a datasource I run from jetty. To do this, I configured a servlet and its mapping thus:
<servlet>
<servlet-name>default </servlet-name>
<servlet-class>org.eclipse.jetty.servlet.DefaultServlet </servlet-class>
<init-param>
<param-name>resourceBase </param-name>
<param-value>/foo/foo </param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>default </servlet-name>
<url-pattern>/* </url-pattern>
</servlet-mapping>
Sadly all I get are 404's. Any help would be much appreciated, btw the rest of my web.xm lfile looks like:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"version="2.5">
<servlet>
<servlet-name>cometd </servlet-name>
<servlet-class>org.cometd.server.continuation.ContinuationCometdServlet </servlet-class>
<load-on-startup>1 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cometd </servlet-name>
<url-pattern>/cometd/* </url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>default </servlet-name>
<servlet-class>org.eclipse.jetty.servlet.DefaultServlet </servlet-class>
<init-param>
<param-name>resourceBase </param-name>
<param-value>/foo/foo </param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>default </servlet-name>
<url-pattern>/* </url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>initializer </servlet-name>
<servlet-class>com.foo.research.Initializer </servlet-class>
<load-on-startup>2 </load-on-startup>
</servlet>
<filter>
<filter-name>cross-origin </filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter </filter-class>
</filter>
<filter-mapping>
<filter-name>cross-origin </filter-name>
<url-pattern>/cometd/* </url-pattern>
</filter-mapping>
</web-app>
I had the same issue; here is a snippet that works (Jetty 6.1.22).
I basically replaced org.eclipse with org.mortbay and removed the
resourceBase parameter (but see below). And this actually ends up in my web.xml file inside my WAR file:
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>foo.bar.MyServlet</servlet-class>
<display-name></display-name>
<description>The smallest Servlet ever!</description>
</servlet>
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.mortbay.jetty.servlet.DefaultServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
Then, you have to put your static files in the "static" directory in your
WAR file. Like this (just to make it clear):
ROOT.war
|_ WEB-INF/
|_ static/
If you want to put your static files elsewhere (but still map them under
the /static/ URI), you can use the resourceBase parameter to specify the
directory, just like you did.
Jetty's documentation helped me to understand this a little bit better:
http://docs.codehaus.org/display/JETTY/Servlets+Bundled+with+Jetty