Reputation: 51
I have followed the tutorial on https://spring.io/guides/gs/producing-web-service/ for creating an app which will receive SOAP requests and should return some SOAP response. However, the namespace in the tutorial is just an example. How can I create my own namespace (URI, prefix, content...)? I have been searching, but not successfully...Thanks in advance!
Upvotes: 1
Views: 1208
Reputation: 24580
As I mentioned in the comment above, you should read this post to understand what an XML namespace is: What are XML namespaces for?.
The gist of it, in the context of your question, is that it provides some context about the elements in your XML. For example, if your service encounters a <table>
element, how does the server know what kind of a <table>
this is?
Is it this?
Is it this?
Or is it this?
Namespaces allow you to differentiate between them by adding a namespace:
<table xmlns="urn:tabular-data:tables">
<table xmlns="urn:chemistry:periodic-table">
<table xmlns="urn:products:furniture:kitchen-table">
You might also want to check this question out to see what I did in here, but in short, I just made those up. I invented them on the spot.
In your example, you can do the same.
The thing is that a SOAP message is an XML composed of different element. Some belong to SOAP itself, some belong to your application, some belong to other web service specifications, etc, and your service needs to make sense of them. That's why you use namespaces, so that you add meaning to the elements. For example, on that page you have this request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:gs="http://spring.io/guides/gs-producing-web-service">
<soapenv:Header/>
<soapenv:Body>
<gs:getCountryRequest>
<gs:name>Spain</gs:name>
</gs:getCountryRequest>
</soapenv:Body>
</soapenv:Envelope>
When you say it's an envelope, you don't mean any envelope, you mean a SOAP envelope. And you know it's a SOAP envelope because its namespace is http://schemas.xmlsoap.org/soap/envelope/
. The SOAP payload itself has elements from the namespace http://spring.io/guides/gs-producing-web-service
.
Both are invented, the only difference is that http://schemas.xmlsoap.org/soap/envelope/
after it was invented it was standardized in a document so that everyone knows what it means. You don't need to standardize yours (http://spring.io/guides/gs-producing-web-service
isn't either), you just need to invent one that is unique and specific to your context.
People usually use domain namespaces that they own, maybe even throw some timestamp in there. So you can use for example http://jovana-vajagic.com/2021/03/20/example-service
if you think that is unique to you. Or you can use urn:uuid:8f4ac50e-574b-4936-b49b-8b129bea945b
if you want to (I got that random UUID from here).
So basically, you just invent one that is fairly unique, and you replace it in the code of that example you are following.
As for defining elements within that namespace, you will need an XML schema where you define your elements, sub-elements, attributes, their types, etc. Look in the tutorial you are following and you will see there is a section on XML schema:
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://spring.io/guides/gs-producing-web-service"
targetNamespace="http://spring.io/guides/gs-producing-web-service"
elementFormDefault="qualified">
...
You need to create something similar, using your namespace (like http://jovana-vajagic.com/2021/03/20/example-service
or whatever you chose) and declare your elements in there.
You will have some reading to do :) Good Luck!
Upvotes: 3