Changing parameters in a JAX-WS web service - java

I'm creating some web services using JAX-WS and the java SE build-in server. Every time I add a new parameter on a web service i need to change the URL it's published to. Otherwise the new parameters always get a null value. How can I make this work without changing the URL?
Here's the main class code with the publishing code:
import javax.xml.ws.Endpoint;
import pickate.AmazonMail;
import pickate.FacebookStream;
class Main {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8888/pickate/amazonmail", new AmazonMail());
Endpoint.publish("http://localhost:8888/pickate/facebookstream", new FacebookStream());
}
}
And the implementation of one of the webservices
package pickate;
import java.util.List;
import javax.jws.Oneway;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
// Other imports go here
#WebService
public class FacebookStream
{
public FacebookStream()
{
}
#WebMethod
#Oneway
public void sendNotification(
#WebParam(name = "receivers") List<String> receivers,
#WebParam(name = "fbtoken") String fbtoken,
#WebParam(name = "body") String body,
)
{
// Some interesting stuff goes here
}
}

It was indeed the client caching up the WSDL file. It seems the PHP Soap Extension (which is what i'm using on the client-side) does it by default.

Related

NoSuchMethod when calling AWS Lambda using the AWS Sdk

I've created & deployed one simple GET API in API Gateway and here is the ARN and there is no authentication whatsoever on this function, I can simply call it on my browser
arn:aws:lambda:ap-southeast-1:XXXXXXXXXXXXXX:function:La
and the public url that can be browsed using the browser is:
https://xxxxxxxxx.execute-api.ap-southeast-1.amazonaws.com/v1/lambda/geta
and I'm using Spring boot project and the below code to invoke the API (Following this Doc)
The interface as the lambda service
package com.xxxxxxx.services.interfaces;
import com.amazonaws.services.lambda.invoke.LambdaFunction;
public interface ILambdaGetBalance {
#LambdaFunction(functionName="La")
String getA();
}
The service using that interface to call the lambda function
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.xxxxxxxx.services.interfaces.ILambdaGetBalance;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.lambda.AWSLambda;
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
import com.amazonaws.services.lambda.invoke.LambdaInvokerFactory;
#Service
public class LambdaService {
#Value("${aws.access-key}")
private String accessKey;
#Value("${aws.secret-key}")
private String secretKey;
#Value("${aws.lambda.region-name}") // this is ap-southeast-1
private String regionName;
public void test() {
AWSCredentials credentials = new BasicAWSCredentials(accessKey,
secretKey);
AWSLambda client = AWSLambdaClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(regionName)
.build();
final ILambdaGetBalance getBalance = LambdaInvokerFactory.builder()
.lambdaClient(client)
.build(ILambdaGetBalance.class);
getBalance.getA();
}
}
after calling the getA function the system will through the following exception:
java.lang.NoSuchMethodError: com.amazonaws.services.lambda.AWSLambdaClient.beforeClientExecution(Lcom/amazonaws/AmazonWebServiceRequest;)Lcom/amazonaws/AmazonWebServiceRequest;
Any idea why is this happening? What am I missing?
Looks like your aws-java-sdk-lambda and aws-java-sdk-core modules may have incompatible versions. How are you resolving the dependencies for your project? The beforeClientExecution method was added to the AmazonWebServiceClient base class in version 1.11.106 of aws-java-sdk-core - see here: https://github.com/aws/aws-sdk-java/blame/master/aws-java-sdk-core/src/main/java/com/amazonaws/AmazonWebServiceClient.java#L590

Endpoint publisher in java does not work

I have the following webservice :
package testSmart;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
#WebService(name="AddHM", portName="prtNameHM", serviceName="srvNameHM", targetNamespace="hm.com")
#SOAPBinding(style=Style.RPC)
public class add {
AddBusinessLogic add=new AddBusinessLogic();
#WebMethod(action="GoAdd", operationName="Go_AddNumber")
public int addNum(int i, int j) {
return add.addNum(i, j);
}
}
which works perfectly fine with glassfish.
then I stopped glassfish and use the following code to make my server :
import javax.xml.ws.Endpoint;
public class publisher {
public static void main(String[] args) {
// TODO Auto-generated method stub
Endpoint.publish("http://localhost:1234/add", new add());
}
}
Now when I try this link:
http://localhost:1234/add
Nothing happens and the browser says no data received.
Even after trying different port the same problem exist.
Can anyone help me how to fix it?
Call to http://localhost:1234/add will not get you anything because there is no specific document with that URI that browser can get. But if you type
http://localhost:1234/add?wsdl
you should get the generated wsdl document.

Grizzly and ServletContainerContext

I'm trying to get hold of some injected context (for example Session or HttpServletRequest) in a Servlet I've written, running on Grizzly, but nothing I do seems to work. The whole process seems to stall rather prematurely with the following error:
SEVERE: Missing dependency for field: javax.servlet.http.HttpServletRequest com.test.server.LolCat.hsr
The server is dead simple, it consists of two files, the static entry point (Main.java):
package com.test.server;
import java.io.IOException;
import java.net.URI;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.grizzly.http.server.HttpServer;
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.ClassNamesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
public class Main {
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/").port(8080).build();
}
public static final URI BASE_URI = getBaseURI();
public static void main(String[] args) throws IOException {
ResourceConfig rc = new ClassNamesResourceConfig(LolCat.class);
HttpServer httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
System.in.read();
httpServer.stop();
}
}
and the serlvet (LolCat.java):
package com.test.server;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
#Path(value = "/lol")
public class LolCat {
#Context HttpServletRequest hsr;
#GET
#Path(value="/cat")
public String list() {
return "meow";
}
}
Specifically, it's the #Context-line in the above source file that is the source and solution to all my problems. I need it, and according to everything I've read about Jersey and Servlets it should work, but alas it does not. I've also tried using GrizzlyWebContainerFactory instead of the GrizzlyServerFactory, but to no avail.
For reference, the project is compiled with the following dependencies:
org.glassfish.grizzly:grizzly-framework:jar:2.2.21
org.glassfish.grizzly:grizzly-http:jar:2.2.21
org.glassfish.grizzly:grizzly-http-servlet:jar:2.2.21
org.glassfish.grizzly:grizzly-http-server:jar:2.2.21
com.sun.jersey:jersey-server:jar:1.17
com.sun.jersey:jersey-servlet:jar:1.17
com.sun.jersey:jersey-core:jar:1.17
javax.servlet:javax.servlet-api:jar:2.5.0
com.sun.jersey:jersey-grizzly2:jar:1.17
com.sun.jersey:jersey-grizzly2-servlet:jar:1.17
asm:asm:jar:3.3.1
This Main class works fine for me:
package com.test.server;
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import java.io.IOException;
import java.net.URI;
import javax.ws.rs.core.UriBuilder;
import com.sun.jersey.api.core.ClassNamesResourceConfig;
import com.sun.jersey.spi.container.servlet.ServletContainer;
import org.glassfish.grizzly.http.server.HttpHandler;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.Request;
import org.glassfish.grizzly.http.server.Response;
import org.glassfish.grizzly.servlet.ServletRegistration;
import org.glassfish.grizzly.servlet.WebappContext;
public class Main {
private static final String JERSEY_SERVLET_CONTEXT_PATH = "";
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost").port(8080).path("/").build();
}
public static final URI BASE_URI = getBaseURI();
public static void main(String[] args) throws IOException {
// Create HttpServer and register dummy "not found" HttpHandler
HttpServer httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, new HttpHandler() {
#Override
public void service(Request rqst, Response rspns) throws Exception {
rspns.setStatus(404, "Not found");
rspns.getWriter().write("404: not found");
}
});
// Initialize and register Jersey Servlet
WebappContext context = new WebappContext("WebappContext", JERSEY_SERVLET_CONTEXT_PATH);
ServletRegistration registration = context.addServlet("ServletContainer", ServletContainer.class);
registration.setInitParameter(ServletContainer.RESOURCE_CONFIG_CLASS,
ClassNamesResourceConfig.class.getName());
registration.setInitParameter(ClassNamesResourceConfig.PROPERTY_CLASSNAMES, LolCat.class.getName());
registration.addMapping("/*");
context.deploy(httpServer);
System.in.read();
httpServer.stop();
}
}
Try http://localhost:8080/lol/cat in your browser.
You can change JERSEY_SERVLET_CONTEXT_PATH to update Servlet's context-path.
As per developers explanations - Grizzly is not fully compliant to JAX-RS 2.0 so there will be no official contexts injections/wrapping. See Jersey Bug-1960
Applicable for Jersey + Grizzly version 2.7+
Luckily there is a way to inject Grizzly request/response objects. Kind of tricky but works
Code sample provided in one of Jersey's unit tests. See Jersey container test
So code fragment will be:
import javax.inject.Inject;
import javax.inject.Provider;
public someclass {
#Inject
private Provider<Request> grizzlyRequestProvider;
public void method() {
if (grizzlyRequestProvider != null) {
Request httpRequest = grizzlyRequestProvider.get();
// Extract what you need
}
}
}
Works fine both for filters and service methods
You can also manually register a ResourceContext
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(getBaseURI());
WebappContext context = new WebappContext("WebappContext", "/api");
ServletRegistration registration = context.addServlet("ServletContainer",
new ServletContainer(config));
registration.addMapping("/*");
context.deploy(httpServer);
Where config is your resource context.
Try something like this :-
public class Main {
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/").port(8080).build();
}
public static void main(String[] args) throws IOException {
ResourceConfig rc = new ResourceConfig().packages("com.example");//path to you class files
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(getBaseURI(), rc);
System.in.read();
httpServer.stop();
}
}

Sharepoint web service throwing Microsoft.SharePoint.SoapServer.SoapServerException

I am coding for consuming Sharepoint 2010 web services in Java using Netbeans. I am able to creating the web service client from WSDL using the provided wizard. When I call the following code I get the Microsoft.SharePoint.SoapServer.SoapServerException
import java.net.Authenticator;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import proxy.webs.GetWebCollectionResponse;
import proxy.webs.GetWebResponse;
import proxy.webs.Webs;
import proxy.webs.WebsSoap;
public class AccessLists {
public static void main(String[] args) throws Exception {
String username = "domain\\Administrator";
char[] password = "password".toCharArray();
NtlmAuthenticator ntlmAuth = new NtlmAuthenticator(username, password);
Authenticator.setDefault(ntlmAuth);
Webs websService = new Webs(new URL("http://servername:7766/_vti_bin/Webs.asmx?wsdl"));
WebsSoap webPort = websService.getWebsSoap();
GetWebResponse.GetWebResult webRes = webPort.getWeb("http://servername/sites/Test1");
System.out.println(webRes);
}
}
The site http://servername/sites/Test1 exists and I can open it in the browser.
Update 1: Similar thing happens for C# code, which I run on the same machine as Sharepoint 2010:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Webs webService = new Webs();
webService.Credentials = System.Net.CredentialCache.DefaultCredentials;
Object o = webService.GetWeb("http://servername/sites/Test1");
Console.WriteLine(o.ToString());
}
}
}
I guess this is the problem with the set up and not with the code.
I was using the wrong endpoint for the web service. For the Sharepoint site http://servername/sites/Test1 the endpoint should also be http://servername/sites/Test1/_vti_bin/Webs.asmx?wsdl

Web Services using jdk 1.6

Hi All
I am new to web services. I have written a java class.
But I am not getting how to deploy it. I mean do i need web server or app server . As this is simple java class i can not make WAR file to deploy it . So what is the method to deploy it and which server should i use. I am using JDK 1.6
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.ws.Endpoint;
#WebService
public class WiseQuoteServer {
#SOAPBinding(style = Style.RPC)
public String getQuote(String category) {
if (category.equals("fun")) {
return "5 is a sufficient approximation of infinity.";
}
if (category.equals("work")) {
return "Remember to enjoy life, even during difficult situatons.";
} else {
return "Becoming a master is relatively easily. Do something well and then continue to do it for the next 20 years";
}
}
public static void main(String[] args) {
WiseQuoteServer server = new WiseQuoteServer();
Endpoint endpoint = Endpoint.publish(
"http://localhost:9191/wisequotes", server);
The best answer to your question would be the tutorial of JAX-WS

Categories

Resources