JAX-WS multiple endpoints Not Found: Invalid Request - web-services

I'm trying implement a web service for two endpoints and getting this error
"404 Not Found: Invalid Request" when tried accessing the service after deploying onto the apache toncat 8.
Below are my web service implementation classes, sun-jaxws.xml and web.xml
WebImplementation1.java
package com.ws.soap.services;
import javax.jws.WebService;
#WebService(endpointInterface = "com.ws.soap.services.WebServiceImpl1")
public class WebServiceImpl1 {
public String printMessage() {
return "Hello from WebServiceImpl1 ";
}
}
WebServiceImplementation2.java
package com.ws.soap.services;
import javax.jws.WebService;
#WebService(endpointInterface = "com.ws.soap.services.WebServiceImpl2")
public class WebServiceImpl2 {
public String displayMessage() {
return "Hello from WebServiceImpl2 ";
}
}
sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint name="WebServiceImpl1" implementation="com.ws.soap.services.WebServiceImpl1"
url-pattern="/impl1" />
<endpoint name="WebServiceImpl2" implementation="com.ws.soap.services.WebServiceImpl2"
url-pattern="/impl2" />
</endpoints>
web.xml
<?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">
<display-name>JAX-WS-Tomcat</display-name>
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>sayhello</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sayhello</servlet-name>
<url-pattern>/impl1</url-pattern>
<url-pattern>/impl2</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>

Using the exact code supplied (plus the JAX-WS RI jars downloaded from https://jax-ws.java.net/), I was able to create a webapp and successfully access the service endpoints /impl1 and /impl2. Be advised the <url-pattern> and <endpoint ... url-pattern="/impl1"> directives state the resource path to the JAX-WS endpoints within the context path of the enclosing web application.
So, if the name of the webapp is MyWebServices (MyWebServices.war with no other files/code than described in the post, deployed to Tomcat 8) and you have <url-pattern>/impl1</url-pattern> in web.xml, and with a default Tomcat instance listening on port 8080, your web service endpoint would be http://localhost:800/MyWebServices/impl1 with the WSDL available via http://localhost:800/MyWebServices/impl1?wsdl
If you want to customize your context path of your webapp (e.g. you don't want /MyWebServices/... you can use the techniques described in this SO question.
For example, my local Tomcat 8 is running on port 8081:

Related

JAX-RS handle multiple authentication mechanism

I'm learning JAX-RS using Jersey and deploy on JBoss EAP 6.3. I'm developing a test application that offers Webservices consumed by different clients. I correctly setted up the basic authentication system based on Roles and username/password (Realm correctly configured and UP&Running). Everything works fine when my client application consume the WS.
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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>testWS</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Realm</realm-name>
</login-config>
<security-constraint>
<web-resource-collection>
<web-resource-name>Admin Pages</web-resource-name>
<url-pattern>/test/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>role-admin</role-name>
</auth-constraint>
</security-constraint>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.providers</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.resources</param-name>
<param-value>false</param-value>
</context-param>
<servlet>
<description>Admin test</description>
<servlet-name>Admin Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.test.webservice.utils.AuthPackageClasses</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Admin Servlet</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
</web-app>
My second step is to consume the same webservices by a web dashboard which require a login form. My idea is to have a free WS to consume the login and return back some token inside the Cookie in the Response; the same token is stored in my Database.
Other WS must be consumed by the web dashboard injecting the Cookie and the server must validate the token stored in the Cookie.
How can I achieve to authenticate the User coming from two different authentication systems?
Should I redesign my whole authentication system and setup a listener:
if ther's a token in the Cookie -> validate the token
otherwise validate basic authentication
Is there a way to achieve this by configuring the web.xml?
What if I want to implement a third authentication system? My problem is to have the same context-root and validate the caller by different ways.

Deployment error with JAX-RS 2.0 RESTful webservice and tomcat 8.0

I am novice to writing REST WebServices.
Currently I am trying to write a RESTful service using jersey-2.x and tomcat 8.0
However, when I try to deploy in eclipse, it gives me error as follows :
java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
What I did is :
wrote below classes :
#ApplicationPath("resources")
public class RestTestApplication extends Application
{
#Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register root resource
classes.add(HelloResource.class);
return classes;
}
}
#Path("sayhello")
public class HelloResource
{
#GET
#Produces("text/plain")
public String sayhello ()
{
return "Hi, How are you !!";
}
}
downloaded from http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jaxrs-ri/2.12/jaxrs-ri-2.12.zip
downloaded jsr311-api-1.1.2.r612.jar
copied all *.jar files from jaxrs-ri-2.12.zip and jsr311-api-1.1.2.r612.jar to WEB-INF/lib and also imported to build path.
Edited web.xml as below :
<?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>RestWS</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>Rest Test</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.example</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Rest Test</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
</web-app>`
run as-->run on server
Getting the error mentioned above.
please let me know what I am doing wrong.
I think its because you have both jars from jaxrs-ri-2.12.zip and jsr311-api-1.1.2.r612.jar in your classpath. jsr311-api-1.1.2.r612.jar has the older implementation of JAX-RS API. Your Application class that your RestTestApplication extends from is from the jsr311-api-1.1.2.r612.jar; however at runtime the Application class from your jaxrs jar in jaxrs-ri-2.12.zip is being referred to. Removing the jsr311 jar from your WEBINF/lib should hopefully resolve the issue.
If you decompile the Application class from both the jars you will notice that the one in jsr311 jar doesn't have getProperties method and hence the java.lang.NoSuchMethodError error.

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.

Restful webservice with jersey 2.0 without maven

Can anybody tell me how to make a restful web service with Jersey 2.0 by not using maven. I have searched everywhere and found tutorial for Jersey1.x versions but not for 2.0. Please help
We provide detail answere based on the user answer user2629427. we checked this on windows 7.
Requirement: (brackets indicate version which this example is tested)
tomcat (8 zip version)
jersey (2.x)
Unzip the tomcat & create a below folder structure in tomcat's 'webapps' folder (folder names are case sensitive).
abc
|___ WEB-INF
|____ classes
|____ lib
Put 'Hello.java' and 'MyApplication.java' into 'classes' folder and 'web.xml' into 'WEB-INF' folder.
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">
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.king.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Myapplication.java
package com.king;
import org.glassfish.jersey.server.ResourceConfig;
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("com.king");
}
}
Hello.java
package com.king;
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 {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
#GET
#Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?><hello>Hello Jersey</hello>";
}
// This method is called if HTML is request
#GET
#Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html><title>Hi Jersey</title><body><h1>Hello Jersey this is laksys</body></h1></html>";
}
}
Unzip jersey and copy all jar files from api, ext, and lib (not folders) into your apps 'lib' folder.
Now compile the two java files using following command
D:\apache-tc-8\webapps\abc\WEB-INF\classes>javac -d . -cp ..\lib\javax.ws.rs-api-2.0.1.jar;..\lib\jersey-server.jar;..\l ib\jersey-common.jar *.java
Next run the tomcat server
D:\apache-tc-8\bin>startup
In browser address bar type this: http://localhost:8080/abc/rest/hello
I found the answer
package com.hellowebservice;
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 {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
#GET
#Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
// This method is called if HTML is request
#GET
#Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<display-name>FirstRestWebService</display-name>
<servlet>
<display-name>Rest Servlet</display-name>
<servlet-name>RestServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.hellowebservice.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
MyApplication.java
package com.hellowebservice;
import org.glassfish.jersey.server.ResourceConfig;
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("com.hellowebservice");
}
}
run with localhost:8080/FirstRestWebService/rest/hello
Just to add to the previous answer. If you aren't using Maven and just building using Eclipse with a Dynamic Web Project and deploying to web app server like Tomcat.
Just download the Jersey JAX-RS 2.0 RI bundle Jersey Downloads, unzip and add all the jars in the lib, api and ext folders to your build path. (I tried without ext jars but got classnotfound when starting the server).
Also add all the jars to the Deployment Assembly of your Dynamic Web Project so they get automatically copied to the WEB-INF/lib directory when deployed to your web app server. Along with the code & web.xml in the above answer, you should have a RESTful api using Jersey 2 up and running.

Build a WS with Spring

I need to create a WS with Spring 3.0.4.RELEASE to run in a Tomcat with Axis2. I'm following this doc: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html#remoting-web-services-jaxws-export-ri (if that paragraph can be called "doc")
Ok, here are the details:
The java class:
package foo;
#WebService(serviceName="MyService")
public class MyService{
#WebMethod
public String getString(){
return "Hello StackOverflow";
}
}
The WEB-INF/spring-ws.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://jax-ws.dev.java.net/spring/core https://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet https://jax-ws.dev.java.net/spring/servlet.xsd">
<wss:binding url="/myService" service="#myService" />
<ws:service id="myService"
impl="foo.MyService" />
</beans>
The WEB-INF/web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="myService" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>my Service</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-ws.xml</param-value>
</context-param>
<!-- this is for Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- these are for JAX-WS -->
<servlet>
<servlet-name>jaxws-servlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jaxws-servlet</servlet-name>
<url-pattern>/myService</url-pattern>
</servlet-mapping>
And last, but not less important, the error when I start tomcat 6.0.29:
Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://jax-ws.dev.java.net/spring/servlet]
Offending resource: ServletContext resource [/WEB-INF/spring-ws.xml]
Someone has any clue of what is happening? Is all the configuration correct? Does anyone have a simple (working) WS to show how to deploy a WS using Spring?
Thanks in advance
I also experience this issue a while back and figured out the problem is with the "https://". Change it back to http:// and you should be good to go. But when you use http:// you get a schema validation error in eclipse because eclipse can't automatically redirect schema url from http:// to https://. And apparently netbeans is capable of it.
One more thing. You'll have to have the xbeans-spring as well. I honestly think that's a pretty stupid dependency.