I am new to jax rs web service. I was studying from this link- http://www.vogella.com/tutorials/REST/article.html
Now when I was trying to do my first rest service i faced some errors.
This is my service code
package de.vogella.jersey.first;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/hello")
public class Hello {
// This method is called if TEXT_PLAIN is request
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
}
My web.xml is
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>de.vogella.jersey.first</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 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>de.vogella.jersey.first</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>
Now when I was running this code I faced HTTP Status 404 error. Please any one help me. I give all the jars from http://jersey.java.net/ Please help me.
If you are using jersey 2.x you Web.xml servlet should be as following
<servlet>
<servlet-name>Jersey Rest Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>de.vogella.jersey.first</param-value>
</init-param>
</servlet>
In jersey 2.x you need to refer "org.glassfish" and your resource URL would be
localhost:your_port/your_app_name/rest/hello
Related
I am trying to create a very simple REST web service using Java, but I can't get the mapping right...
This is my service:
package rest;
#Path("/square/{num}")
public class SquareNumberRest {
#GET
#Produces(MediaType.APPLICATION_JSON)
public String squareNum(#PathParam("num") String num) {
int n = Integer.parseInt(num);
int squared = n * n;
JSONObject jo = new JSONObject(squared);
return jo.toString();
}
}
My web.xml descriptor:
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>TestProject</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 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>rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
I try to consume the service on the following URL:
http://localhost:8080/MyProjectName/square/3
but I'm getting a 404 error.
Did you define the URL context for the project? It's possible that the URL root for the project is right on localhost:8080, which means you'll need to do
http://localhost:8080/square/3 instead.
I am just Rest API concept with hello world program. I'm following some tutorial videos and trying the same program But I'm not getting the expected result.
Here is my Book.java
package com.book;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/book") //URI
public class Book {
#GET
#Produces(MediaType.TEXT_XML)
public String sayHelloXML() {
String response = "<?xml version='1.0'?>" + "<hello>Hello World</hello>";
return response;
}
}
and the web.xml
<?xml version="1.0" encoding="UTF-8"?>
<element>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>WSdemo</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>JAVA WS</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>book</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAVA WS</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
</element>
And I'm using Tomcat v8.0 server.
But as I run the application, I am getting out put as
Can somebody give me key ideas for learning the concept of RESTfull web service?
There should be correct configuration in web.xml
I've edited my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- <element> -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>WSdemo</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>JAVA WS</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.book</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAVA WS</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
<!-- </element> -->
And I got the correct output as
Thank You.
I am trying to learn building rest web service watching videos of Java Brains. Instead of just following everything the tutor does I try to mix it up on my own. So, I was facing this problem when I built up a new service class on a new package but kept on getting the error Servlet.init() for servlet Second Jersey REST Service threw exception. Would be glad if someone explained why? I am using a netbeans IDE.
Here's my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<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.services.messages</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--New Part added -->
<servlet>
<servlet-name>Second 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.services.profiles</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!--New part added -->
<servlet-mapping>
<servlet-name>Second Jersey REST Service</servlet-name>
<url-pattern>/rest2/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>NewServlet</servlet-name>
<servlet-class>com.servlets.myclass.NewServlet</servlet-class>
<init-param>
<param-name>Name</param-name>
<param-value>Value</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>NewServlet</servlet-name>
<url-pattern>/NewServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
and here's my new web service class:
package com.service.profiles;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/profiles")
public class ProfileClass {
public ProfileClass()
{
}
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getX()
{
return "Got it";
}
}
im following one of the online tutorials for making rest api in java. I work in Eclipse 4.5.2. When i enter http://localhost:7001/ds2/api/v1/status i got mentioned in a title error. I included Jersey 1.19. Im using Oracle WebLogic Server 12c R2 (12.2.1). Starting page(index) works fine, but anything i would enter after /api/ gives me the same error.
My V1_status.java class:
package ds2.status;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
#Path("/v1/status")
public class V1_status {
#GET
#Produces(MediaType.TEXT_HTML)
public String returnTitle() {
return "<p>Java Web Service</p>";
}
}
My web.xml:
<?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>ds2</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 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>ds2</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
weblogic.xml:
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app" 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_3_0.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.8/weblogic-web-app.xsd">
<wls:weblogic-version>12.2.1</wls:weblogic-version>
<wls:context-root>ds2</wls:context-root>
</wls:weblogic-web-app>
What can cause it or maybe how can i investigate by myslef whats wrong?
I am developing REST API. I am using Jersey framework. I set up my project correctly. But when I hit URLs to the browser, no response is coming from server. Also nothing is logged in the server console. No warning no error.
My project hierarchy is
Web.xml
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>com.sun.rest</display-name>
<welcome-file-list>
<welcome-file>readme.html</welcome-file>
<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 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.sun.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
Status.java
package com.sun.rest;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
#Path("/v1/status/*")
public class Status {
private static final String api_version = "00.01.00"; //version of the api
#GET
#Produces(MediaType.TEXT_HTML)
public String returnTitle() {
return "<p>Java Web Service</p>";
}
#Path("/version")
#GET
#Produces(MediaType.TEXT_HTML)
public String returnVersion() {
return "<p>Version:</p>" + api_version;
}
}
Response
You don't need /*/ in your path. If you remove that from your path, that would resolve your issue.