I am creating a RESTful web service with jersey 2.0, here is my web.xml file:
<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>Rest</servlet-name>
<servlet-class>
com.shop.domain.ShoppingApplication
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
My ShoppingApplication class:
public class ShoppingApplication extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(CustomerResource.class);
return s;
}
}
And my CustomerResource class:
#Path("/customers")
public class CustomerResource{
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getCustomer(){
return "Hello";
}
}
When running with localhost:8080/customers, I got a 404 not found page, how should I fix it.
Using JAX-RS inside a non-JEE6 container requires you to provide a JAX-RS Servlet (like Jersey) to be mapped within your web.xml
Change your web.xml to
<web-app>
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.shop.domain.ShoppingApplication</param-value>
</init-param>
</servlet>
...
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
...
</web-app>
See https://jersey.java.net/documentation/latest/deployment.html#deployment.servlet.2 for docs.
Related
I am just testing out my first Jersey filter in my REST API. Please check the below code.
AuthenticationFilter - The Jersey Filter
#Secured
#Provider
#Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter implements ContainerRequestFilter{
// #Context
// private ResourceInfo resourceInfo;
public AuthenticationFilter()
{
System.out.println("test printing");
}
#Override
public void filter(ContainerRequestContext crc) throws IOException
{
String headerString = crc.getHeaderString("Bearer");
System.out.println("bluh: "+headerString);
}
}
Secured - Just an interface for Secured NameBinding annotation
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import javax.ws.rs.NameBinding;
#NameBinding
#Retention(RUNTIME)
public #interface Secured {}
UserJsonService - Rest Call
#Path ("/user")
public class UserJSONService {
#GET
#Path("/test")
#Produces(MediaType.APPLICATION_JSON)
#Secured
public Response test()
{
return Response.status(Response.Status.OK).entity("sd").build();
}
}
The problem here when I fire the test REST call, the filter is not getting triggered at all. I know that because nothing is getting printed in console as I wanted it to do.
I have made no changes in my web.xml file because I used annotation, but as for your information it is like below.
<?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>ExampleServlet</servlet-name>
<servlet-class>test.ExampleServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Jersey RESTful 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>rest</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ExampleServlet</servlet-name>
<url-pattern>/ExampleServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Jersey RESTful Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
What is wrong here?
Both filter and service/resource should be present in the rest package or one of its sub folders. It is also possible to specify multiple packages delimited by ';'. For example: rest;filter.
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";
}
}
I am considerably new to Spring. What I know is that Spring boot has an embedded tomcat server which can make my Web Service run on a defined host:port.
I prefer to use an independent configurable JBoss instead and make a WS.
I am not sure about the structure of that project, neither do I have a bit of idea what type of context to be declared where!
This is the project structure that I have created. Not sure if this is correct.
JBoss runs fine but on each request, I am getting 404
Here is the 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" 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>AdobeSystems</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>application</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
And here is my controller code:
#RestController
public class CalculateControllerImpl implements CalculateController{
private CalculateService calculateService;
private DataObjectSRO dataObjectSRO;
public CalculateControllerImpl(){
calculateService = new CalculateServiceImpl();
}
#RequestMapping("/add")
#ResponseStatus(HttpStatus.OK)
public #ResponseBody int add(#RequestParam(value="first", defaultValue="10") int first,#RequestParam(value="second", defaultValue="5") int second ) {
dataObjectSRO.setFirst(first);
dataObjectSRO.setSecond(second);
return calculateService.add(dataObjectSRO.getFirst(),dataObjectSRO.getSecond());
}
}
I wrote a simple application using vaadin.
#Title("PMC")
#Component("pmcVaadin")
#Scope("prototype")
public class PmcUi extends UI {
#Autowired
private ContentLayout contentLayout;
#Autowired
private TabContent tabs;
#Override
protected void init(VaadinRequest request) {
VerticalLayout mainLayout = new VerticalLayout();
// mainLayout.addComponent(contentLayout.getContent());
mainLayout.addComponent(tabs.createTab("Projects", "Developers"));
mainLayout.setSizeFull();
setContent(mainLayout);
}
}
Tabs - my TabContent component, extended from TabSheet.
But when I click on the tab I've got an error:
Communication problem Take note of any unsaved data, and click here or press ESC to continue. *UIDL could not be read from server. Check servlets mappings. Error code: 404*
My web.xml looks like this
<?xml version="1.0"?>
<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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/pmc-web-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>another-pmc-servlet</servlet-name>
<servlet-class>ru.xpoft.vaadin.SpringVaadinServlet</servlet-class>
<init-param>
<param-name>beanName</param-name>
<param-value>pmcVaadin</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>pmc-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>another-pmc-servlet</servlet-name>
<url-pattern>/VAADIN/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>pmc-servlet</servlet-name>
<url-pattern>/JSP/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>/WEB-INF/jsp/*</url-pattern>
</servlet-mapping>
</web-app>
And I get access to the page using URL http://localhost:8080/pmc-web/VAADIN.
In order to resolve the problem I added mapper for /* to vaadin servlet.
<servlet-mapping>
<servlet-name>another-pmc-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
When do I need to include the following snippet in web.xml in order to activate Jersey?
<servlet>
<servlet-name>Jersey App</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.company.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Is it App. Server dependent if Jersey is activated by default or needs this web.xml entry?
If an App. Server does not require the code above, can I configure the servlet-mapping path, like:
<servlet-mapping>
<servlet-name>Jersey App</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
I am just summarizing the Jersey 2.x docs sent by Bhesh in comment:
Servlet 2.x Container
(Java EE 5, Glassfish 2.x), you need to hook Jersey as a Servlet.
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
...
</init-param>
</servlet>
...
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/myApp/*</url-pattern>
</servlet-mapping>
or a Servlet Filter:
<filter>
<filter-name>MyApplication</filter-name>
<filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
<init-param>
...
</init-param>
</filter>
...
<filter-mapping>
<filter-name>MyApplication</filter-name>
<url-pattern>/myApp/*</url-pattern>
</filter-mapping>
Servlet 3.x Container
Descriptorless case
(Java EE 6, 7; Glassfish 3, 4)There is no need for web.xml configuration for the simplest case. Instead an custom ResourceConfig class is needed with #ApplicationPath annotation for configuration:
#ApplicationPath("resources")
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("org.foo.rest;org.bar.rest");
}
}
Configuring with web.xml
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/myresources/*</url-pattern>
</servlet-mapping>
</web-app>
In this case all the root resource classes (the classes annotated with #Path annotation) as well as any providers that are annotated with #Provider annotation packaged with the application will be automatically registered in the JAX-RS application.