I am developing a restful web service in Java. One of my service needs to call a SOAP service and for this I need to build an XML request. My question is that "I want to build that xml file in a seperate helper class, is this a convenient way to do? " I am using Spring for MVC, is there any advantages of it here that I can implement?
Example pseucode;
#RestController
#RequestMapping("/rest/menu")
public class MenuController {
#RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> createUser(#RequestBody JSONObject userInfo){
//Here I need to make a Soap call to retrieve some information
MyXMLBuilder myXMLBuilder = new MyXMLBuilder();
String soapRequest = myXMLBuilder.build();
}}
and here the helper class;
public class MyXMLBuilder(){
public String build(){
//xml build implementation
}}
Usually, to consume a SOAP WebService, you will create the required stubs with the wsimport tool, which is within your Java SDK in the /bin folder.
Within your favourite terminal, switch to your desired folder for the stubs to be created in and type:
wsimport -keep -verbose http://example.com/myservice?wsdl
You can then use the Service Stubs like this
MyServiceImplService myService = new MyServiceImplService();
MyService port = myService.getMyServiceImplPort();
port.yourFunction(param1, param2);
Related
I have a Java project with various #RequestMapping annotations.I want to make a new project which can use this #RequestMapping,is that possible
Of course you can.
If I understand your question correctly, do you want to use the data provided by a Spring application in another application?
It's not that hard, you just have to keep a few things in mind.
The applications have to run on different ports, of course both applications have to be started.
For example, App1 has a #RequestMapping #GetMapping for personal data.
The path is e.g. http://localhost:8080/persondata
In the second application, you only need to address the API endpoint if you need this data.
This can be done with RestTemplate, for example.
#RestController
#RequestMapping("/persondata")
class PersonDataRestController {
private final Service personService;
public PersonDataRestController(Service personService) {
this.personService = personService;
}
#GetMapping
public ResponseEntity<Collection<?>> getAllPersonData() {
return ResponseEntity.ok(personService.allPersonData());
}
}
You just have to replace the Service with your PersonService or whatelse.
in the second application you can call the REST endpoint with RestTemplate for example
RestTemplate template = new RestTemplate();
try{
ResponseEntity<ArrayList<?>> response = template.exchange("http://localhost:8080/persondata", HttpMethod.GET, null, new ParameterizedTypeReference<ArrayList<?>>() {});
In terms of the specific application, you may need a DTO object.
For this topic i can suggest you this website
I hope I could answer your question
I want to make an HTTP request with Quarkus. However, the target URL is not known at compilation time, it will be composed from different parts at runtime.
Quarkus provides a way to build static REST clients like this:
#Path("/v2")
#RegisterRestClient
public interface CountriesService {
#GET
#Path("/name/{name}")
#Produces("application/json")
Set<Country> getByName(#PathParam String name);
}
However, I am loking for something like the Python requests package:
url = 'https://stackoverflow.com/questions/ask'
response = requests.get(url)
I am building the application in Kotlin, so all Java and Kotlin libraries should work.
What should I use?
With the MP REST Client defined in an interface, you can use the programmatic client creation API:
CountriesService remoteApi = RestClientBuilder.newBuilder()
.baseUri("created at runtime url")
.build(CountriesService.class);
remoteApi.getByName("");
I've defined a RESTful WebService (by using RESTEasy on JBoss AS 7) that consumes a JSON data stream.
#PUT
#Path("/send")
#Consumes(MediaType.APPLICATION_JSON)
public Response consumeJSON(Student student) {
String output = student.toString();
// Do something...
return Response.status(200).entity(output).build();
}
How can I call my WS from another Spring-based webapp, by properly using the RestTemplate, mapping a Java Object to JSON and passing it as request body?
Note: I'm asking about Spring with the aim to investigate the facilities provided by the framework. I well know that it is possible to do that by defining manually the request body.
Cheers, V.
In the client application, you can create an interface with the same signature as the one you expose on the server side, and the same path.
Then, in the spring configuration file, you can use the RESTeasy client API to generate a proxy connecting to the exposed webservice.
In the client application, it would look like this :
SimpleClient.java
#PUT
#Path("/send")
#Consumes(MediaType.APPLICATION_JSON)
public Response consumeJSON(Student student);
Config.java
#Bean
public SimpleClient getSimpleClient(){
Client client = ClientFactory.newClient();
WebTarget target = client.target("http://example.com/base/uri");
ResteasyWebTarget rtarget = (ResteasyWebTarget)target;
SimpleClient simple = rtarget.proxy(SimpleClient.class);
return simple;
}
Then, in the place where you want to invoke this web service, you inject it with Spring and you can call the method. RESTeasy will search for the webservice matching with with your client (according to the path and the request type) and will create a connection.
Launcher.java
#Resource
private SimpleClient simpleClient;
public void sendMessage(Student student) {
simpleClient.consumeJSON(student);
}
Docs on the RESTesay client API : http://docs.jboss.org/resteasy/docs/3.0.7.Final/userguide/html/RESTEasy_Client_Framework.html
Hope this was helpfull.
i have a web service working like this:
#WebService(serviceName = "TempService")
public class TempService {
#WebMethod(operationName = "addBarkod")
public Boolean addBarkod(#WebParam(name = "barkod") Barkod barkod) {
System.out.println(barkod.getBarkodNo());
}
}
and the Barkod class as:
public class Barkod {
private String barkodNo;
// there are constructors and getters, setters etc. nothing fancy //
}
with this structure my web service can be called with soapUI wtihout a problem. the problem is when i want to annotate my model class with JAXB annotations like:
#XmlType(name="barkod")
#XmlRootElement(name="barkod")
#XmlAccessorType(XmlAccessType.FIELD)
i can deploy this to glassfish 3.1 and soapUI generates new client request with new structure but when it comes to do "barkod.getBarkodNo();" at addBarkod operation it throws a NullPointerException. i looks like the XML i sent to the web service does not create a proper Barkod object.
do i have to do with web service class or something?
i think problem caused because soapUI generates the request automatically from my WSDL. when i annotate my class with #XmlRootElement without the namespace clause it does not map given XML to may object. i assign #XmlRootElemen(namespace="") and my problem goes away.
thanks for responses anyway.
In IntelliJ 10.0.3
I use the menu option "new web service" and this generates a class file and adds to sun-jaxws.xml - this is fine - it's working.
Now if I try to write a Java client for this web service I get IllegalArgumentException TestWebService is not an interface
Here's my client code:
public class WebServiceTest {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost/services/TestWebService?wsdl");
//1st argument service URI, refer to wsdl document above
//2nd argument is service name, refer to wsdl document above
QName qname = new QName("http://ws.mydomain.com/", "TestWebServiceService");
Service service = Service.create(url, qname);
TestWebService test = service.getPort(TestWebService.class); // fails here
System.out.println(test.sayHelloWorldFrom("TESTING...."));
}
}
How should I implement this? Should I have an interface and a class? Is there a good example? Best practice?
this is my endpoint definition in sun-jaxws.xml
<endpoint
name='TestWebService'
implementation='com.allscripts.ws.TestWebService'
url-pattern='/services/TestWebService'/>
I was getting messed up because I was trying to use the web service withing my application using the same classpath. Running a test in a different java project works fine.