I've problem with calling java endpoint (code below) from perl client (activePerl 5.16).
Those code snippets are from book Java Web Services Up And Running
package ch01.ts;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
#WebService
#SOAPBinding(style=Style.RPC)
public interface TimeServer {
#WebMethod
String getTimeAsString();
#WebMethod
long getTimeAsElapsed();
}
package ch01.ts;
import java.util.Date;
import javax.jws.WebService;
#WebService(endpointInterface="ch01.ts.TimeServer")
public class TimeServerImpl implements TimeServer {
public String getTimeAsString() {
return new Date().toString();
}
public long getTimeAsElapsed() {
return new Date().getTime();
}
}
package ch01.ts;
import javax.xml.ws.Endpoint;
public class TimeServerPublisher {
public static void main(String[] args) {
Endpoint.publish("http://127.0.0.1:9876/ts", new TimeServerImpl());
}
}
And the perl consumer:
use SOAP::Lite;
my $url = 'http://127.0.0.1:9876/ts?wsdl';
my $service = SOAP::Lite->service($url);
print "\nCurrent time is: ",$service->getTimeAsString();
print "\nElapsed miliseconds from the epoch: ", $service->getTimeAsElapsed();
When I'm calling the web service I'm having this stack trace:
maj 04, 2013 10:21:40 AM com.sun.xml.internal.ws.transport.http.HttpAdapter$HttpToolkit handle
SEVERE: Couldn't create SOAP message. Expecting Envelope in namespace http://schemas.xmlsoap.org/soap/envelope/, but got http://schemas.xmlsoap.org/wsdl/soap/
com.sun.xml.internal.ws.protocol.soap.VersionMismatchException: Couldn't create SOAP message. Expecting Envelope in namespace http://schemas.xmlsoap.org/soap/envelope/, but got http://schemas.xmlsoap.org/wsdl/soap/
at com.sun.xml.internal.ws.encoding.StreamSOAPCodec.decode(Unknown Source)
I think that the soap version is the problem, above example is from 1.1, when I've change the client code to
my $service = SOAP::Lite->service($url)->soapversion('1.2');
then different error is throw
com.sun.xml.internal.ws.server.UnsupportedMediaException: Unsupported Content-Type: application/soap+xml; charset=utf-8 Supported ones are: [text/xml]
I need help with either dealing with envelope problem or content-type. I will be grateful for any directions, code and anything else that could help.
I am not quite sure of Perl->Soap API, But for first case where client version is 1.1 may be you need to mention namespace also somewhere.
May be like
server->setNamespace() //or
SOAP::Lite->service($url,"<namespace>"); //please search for perl web service client examples
And for second case(1.2) service is expecting text and your api sends soap encoding or something.
Refer http://www.herongyang.com/Web-Services/Perl-SOAP-1-2-Unsupported-Media-Type-application-soap.html
This may be helpful
my $client = SOAP::Lite->new()
->soapversion('1.2')
->envprefix('soap12')
->default_ns('http://xmlme.com/WebServices')
->on_action( sub {join '/', #_} )
->readable(true)
->proxy('http://www.xmlme.com/WSShakespeare.asmx');
and
http://www.herongyang.com/Web-Services/Perl-SOAP-1-2-Request-Differences-SOAP-1-1-and-1-2.html
Hope it helps
Related
I am trying to send an array of strings using axios to a rest endpoint using jersey with spring boot and tomcat. It results in a 404 and I am clueless. The url being passed is correct because if I send a single string with GET it works correctly.
EDIT: Any exception within any another endpoint (say NullPointerException) also results in 404 being shown in the browser. This is something to do with the configuration. So 404 is kind of a red herring status code.
This is my java side code
import org.springframework.stereotype.Component;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.*;
#Component
#Path("v1/")
public class SomeResourceV1 {
#POST
#Path("delete")
#Consumes(MediaType.APPLICATION_JSON)
public void deleteFoo(List<String> ids) {
if (ids != null) {
// do something
}
}
}
This is the typescript code:
public delete(someIds : string[]) {
axios({
method: 'POST',
url: "/v1/delete",
data: someIds
}).then((response : any) => {
}).catch((error) => {
console.log("*** delete error ***", error);
});
}
I have also tried to send data as
data:{
ids: someIds
}
I have also attempted to use transformRequest but to no avail.
How does I fix this? Thanks for the help!
I have a java app exposing an SOAP API through Jetty. I can successfully access my WSDL and forge a request, but the webparam sent is always null. I don't know how to debug this problem.
Here i have a few snippets of the functions involved in the request.
I'll edit if you need more information:
#WebMethod(
operationName = "findEvent"
)
public ServiceEventDto findEvent(
#WebParam(name = "eventId") Long eventId) throws InstanceNotFoundException {
Event event
= EventServiceFactory.getService().findEvent(eventId);
return EventToEventDtoConversor.toEventDto(event);
}
This is the request:
<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:eve="http://ws.udc.es/event">
<x:Header/>
<x:Body>
<eve:findEvent>
<eve:eventId>0</eve:eventId>
</eve:findEvent>
</x:Body>
Thank you in advance.
I believe the problem is that your SOAP input is using the eve namespace prefix for the eventId input element.
Try this:
<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:eve="http://ws.udc.es/event">
<x:Header/>
<x:Body>
<eve:findEvent>
<eventId>0</eventId>
</eve:findEvent>
</x:Body>
I was able to recreate using the following service provider in Jetty 9.4:
Service endpoint interface:
package org.example.sampleservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
#WebService(targetNamespace="http://ws.udc.es/event")
public interface SampleService {
#WebMethod(operationName = "findEvent")
public ServiceEventDto findEvent(#WebParam(name = "eventId") Long eventId) throws InstanceNotFoundException;
}
Service implementation:
package org.example.sampleservice;
import javax.annotation.Resource;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
#WebService(endpointInterface = "org.example.sampleservice.SampleService", targetNamespace="http://ws.udc.es/event")
public class SampleServiceImpl implements SampleService {
#Resource
private WebServiceContext ctx;
#WebMethod(operationName = "findEvent")
public ServiceEventDto findEvent(#WebParam(name = "eventId") Long eventId) throws InstanceNotFoundException {
System.out.println("SampleServiceImpl: received eventId " + eventId);
return new ServiceEventDto();
}
}
When I use your original input with <eve:eventId>0</eve:eventId> I observe the following output:
SampleServiceImpl: received eventId null
When I use <eventId>0</eventId> I observe the expected output:
SampleServiceImpl: received eventId 0
If, however, you are expected to accept <eve:eventId> you could also adjust your #WebParam to add targetNamespace as follows:
#WebParam(name = "eventId", targetNamespace="http://ws.udc.es/event") Long eventId
When I change my service provider this way, the output is reversed and <eve:eventId> is no longer null.
I am new to writing Java client for Restful API using Apache CXF.
On running below code I am getting error 415 returned which when I looked online shows as "unsupported media type". In order to fix it I changed the code to "target.request(MediaType.APPLICATION_XML)" from original target.request(). However this didn't fix the code.
What is the best way to debug this issue?
Thanks a lot in advance for your time.
Update: After discussion with the Rest API developer I came to know that I need to add a header "("Content-Type", "application/x-www-form-urlencoded");". but I am not sure how to add a header. Does anyone know how to add this header here?
package com.blackhawk.ivr.restAPI.client;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
public class BlissRestAPI {
public static final String BLISS_SERVICRE_URL = "http://x.x.x.x:9090/services";
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(BLISS_SERVICRE_URL);
target = target.path("/cardmanagementservices/v3/card/status").queryParam("ani", "xxxxxxxxxx").queryParam("card.expiration", "xxxxxx").queryParam("card.number", "xxxxxxxxxxxxxxxx").queryParam("channel.id", "xyz");
Invocation.Builder builder = target.request(MediaType.APPLICATION_XML);
Response response = builder.get();
System.out.println(response.getStatus());
response.close();
client.close();
}
}
First you can change the media type as given below.
Client: MediaType.APPLICATION_XML
Rest: MediaType.APPLICATION_JSON
JAX-WS are Java standard to build web service. So you have used it here, As my knowledge it is easy to use axis 2 to this kind of web services and clients since there are more implementations of JAX-WS. So i will give you a solution using apache axis technology.
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;
public class axisClient {
public static void main(String [] args) throws Exception {
String endpoint = "http://localhost:8090/archive_name/service_name.jws";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName( "service_method_name" );
call.addParameter("parameter_name", XMLType.XSD_STRING, ParameterMode.IN );
call.setReturnType( XMLType.XSD_STRING );
call.setProperty(Call.CHARACTER_SET_ENCODING, "UTF-8");
String jsonString = (String) call.invoke( new Object [] { "parameter_value"});
System.out.println("Got result : " + jsonString);
}
}
I got it working by using below code (got 200 status returned)
WebClient client = WebClient.create(BLISS_SERVICRE_URL);
client.path("/cardmanagementservices/v3/card/status").query("ani", "xxxxxxxxxx").query("card.expiration", "xxxxxx").query("card.number", "xxxxxxxxxxxxxx").query("channel.id", "xxxxx");
client.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML);
client.header("Content-Type","application/x-www-form-urlencoded");
Response response = client.get();
System.out.println(response.getStatus());
I'm working on an Android application that uses Retrofit to create a restful client. In order to debug networks calls, I would like to display or dump the url that's actually being invoked. Is there a way to do this? I've included some code below which shows how the app currently using retrofit.
Client interface definition:
import retrofit.Callback;
import retrofit.http.Body;
import retrofit.http.GET;
import retrofit.http.Headers;
import retrofit.http.POST;
import retrofit.http.Path;
// etc...
public interface MyApiClient {
#Headers({
"Connection: close"
})
#GET("/{userId}/{itemId}/getCost.do")
public void get(#Path("userId") String userId, #Path("itemId") String userId, Callback<Score> callback);
//....etc
}
Service which uses generated client:
// etc...
import javax.inject.Inject;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
#Inject
MyApiClient myApiClient;
// etc...
myApiClient.getCost(myId, itemId, new Callback<Cost>() {
#Override
public void success(Cost cost, Response response) {
Log.d("Success: %s", String.valueOf(cost.cost));
if (cost.cost != -1) {
processFoundCost(cost);
} else {
processMissingCost(itemId);
}
stopTask();
}
#Override
public void failure(RetrofitError error) {
handleFailure(new CostFailedEvent(), null);
}
});
}
call.request().url(), where call is type of retrofit2.Call.
RetrofitError has a getUrl() method that returns the URL.
Also the Response has a getUrl() method as well within the callback.
That, and you can also specify the log level as per this question:
RestAdapter adapter = (new RestAdapter.Builder()).
//...
setLogLevel(LogLevel.FULL).setLog(new AndroidLog("YOUR_LOG_TAG"))
Although based on the docs, LogLevel.BASIC should do what you need.
BASIC
Log only the request method and URL and the response status code and execution time.
Yes, you can enable debug logging by calling setLogLevel() on your RestAdapter.
I typically set logging to LogLevel.FULL for debug builds like so:
RestAdapter adapter = builder.setEndpoint("example.com")
.setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
.build();
This will automatically print out all of the information associated with your HTTP requests, including the URL you are hitting, the headers, and the body of both the request and the response.
I am developing my first Web-Service at the moment.
Client is developed with JavaScript.
My problem is that it did not work. I do not know what my problem is.
I think it is a mistake on the client site.
I tried it with an Java Web-Service Client and there it works.
Web-Service:
import javax.jws.*;
import javax.jws.soap.SOAPBinding;
#WebService(name="TicketWebService", targetNamespace = "http://my.org/ns/")
#SOAPBinding(style = SOAPBinding.Style.RPC)
public class TicketWebService {
#WebMethod(operationName="getContact")
public String getContact()
{
return "Hallo Hans!!!";
}
}
Publish on Server:
import javax.swing.JOptionPane;
import javax.xml.ws.Endpoint;
public class PublishWsOnServer
{
public static void main( String[] args )
{
Endpoint endpoint = Endpoint.publish( "http://localhost:8080/services",
new TicketWebService() );
JOptionPane.showMessageDialog( null, "Server beenden" );
endpoint.stop();
}
}
Client:
<html>
<head>
<title>Client</title>
<script language="JavaScript">
function HelloTo()
{
var endpoint = "http://localhost:8080/services";
var soapaction = "http://localhost:8080/services/getContact";
xmlHttp = getXMLHttp();
xmlHttp.open('POST', endpoint, true);
xmlHttp.setRequestHeader('Content-Type', 'text/xml;charset=utf-8');
xmlHttp.setRequestHeader('SOAPAction', soapaction);
xmlHttp.onreadystatechange = function() {
alert(xmlHttp.responseXML);
}
xmlHttp.send(request);
}
</script>
</head>
<body onLoad="HelloTo()" id="service">
Body in Client
</body>
</html>
The alert does not work...
I'm pretty new at JAX-WS but I think that maybe your problem is not in the client side. First of all, here you have a HelloWorld example that works fine, if you look into the code you will see that in the web service implementation the annotation WebService is defined as
#WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
which is the full package of your "TicketWebService". Another difference is that the example defines an interface (marked with the #WebService annotation) and then implements it, including the #WebService also in the implementation. I don't think this is mandatory, but is a good practice to define the interface.