我试着写一个程序,它只是检查一个增值税号码的有效性。我想在这里使用的web服务:https://ec.europa.eu/taxation_customs/vies/services/checkVatService.wsdl我遵循了这个教程(https://www.baeldung.com/java-soap-web-service),并得到maven生成一些类在我的项目中与以下pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ffleuchten.vatchecker</groupId>
<artifactId>VatChecker</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>jakarta.xml.ws</groupId>
<artifactId>jakarta.xml.ws-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>4.0.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-ri</artifactId>
<version>4.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<wsdlUrls>
<wsdlUrl>https://ec.europa.eu/taxation_customs/vies/services/checkVatService.wsdl</wsdlUrl>
</wsdlUrls>
<keep>true</keep>
<packageName>com.ffleuchten.vatchecker.generated</packageName>
<sourceDestDir>src/main/java</sourceDestDir>
</configuration>
</plugin>
</plugins>
</build>
字符串
生成的类如下:
-rw-r--r-- 1 manoca manoca 2.2K Nov 15 15:15 CheckVat.java
-rw-r--r-- 1 manoca manoca 6.8K Nov 15 15:15 CheckVatApprox.java
-rw-r--r-- 1 manoca manoca 13K Nov 15 15:15 CheckVatApproxResponse.java
-rw-r--r-- 1 manoca manoca 6.4K Nov 15 15:15 CheckVatPortType.java
-rw-r--r-- 1 manoca manoca 5.2K Nov 15 15:15 CheckVatResponse.java
-rw-r--r-- 1 manoca manoca 3.1K Nov 15 15:15 CheckVatService.java
-rw-r--r-- 1 manoca manoca 4.9K Nov 15 15:15 ObjectFactory.java
-rw-r--r-- 1 manoca manoca 221 Nov 15 15:16 package-info.java
型
生成这些类之后,就有点奇怪了,因为生成的import语句是错误的,生成了无效的导入,比如:
import javax.xml.ws.Service;
型
我让我的Eclipse改成了
import jakarta.xml.ws.Service;
型
进口
在那之后,一切都编译得很好,我做了这个类来尝试它:
package com.ffleuchten.vatchecker;
import java.time.LocalDate;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import com.ffleuchten.vatchecker.generated.CheckVatPortType;
import com.ffleuchten.vatchecker.generated.CheckVatService;
import jakarta.xml.ws.Holder;
public class TestVAT {
private String countryCode;
private String vatNumber;
public TestVAT(String countryCode, String vatNumber) {
this.countryCode = countryCode;
this.vatNumber = vatNumber;
}
private void run() {
try {
System.out.println("checking VAT number "
.concat(this.vatNumber)
.concat(" with country code ")
.concat(this.countryCode)
.concat("..."));
CheckVatService service = new CheckVatService();
CheckVatPortType port = service.getCheckVatPort();
Holder<String> countryCodeHolder = new Holder<String>(this.countryCode);
Holder<String> vatNumberHolder = new Holder<String>(this.vatNumber);
Holder<XMLGregorianCalendar> requestDateHolder = new Holder<XMLGregorianCalendar>(DatatypeFactory.newInstance().newXMLGregorianCalendar(LocalDate.now().toString()));
Holder<Boolean> validHolder = new Holder<Boolean>(false);
Holder<String> nameHolder = new Holder<String>("?");
Holder<String> adressHolder = new Holder<String>("?");
port.checkVat(countryCodeHolder, vatNumberHolder, requestDateHolder, validHolder, nameHolder, adressHolder);
System.out.println("VAT number "
.concat(vatNumberHolder.value)
.concat(" with country code ")
.concat(countryCodeHolder.value)
.concat(" is ")
.concat(validHolder.value.booleanValue() ? "valid" : "invalid")
.concat(" and belongs to ")
.concat(nameHolder.value)
.concat(" who lives at ")
.concat(adressHolder.value)
.concat("!"));
System.out.println("VAT number checked! exiting now...");
System.exit(0);
} catch (DatatypeConfigurationException e) {
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) {
if(args.length < 1) {
System.err.println("no country code passed as parameter!");
System.exit(1);
}
if(args.length < 2) {
System.err.println("no VAT number passed as parameter!");
System.exit(1);
}
String countryCode = args[0];
String vatNumber = args[1];
new TestVAT(countryCode, vatNumber).run();
}
}
型
它仍然可以编译甚至运行,但是无论我使用什么VAT号码和/或国家代码,我总是得到以下输出:
checking VAT number DE814416365 with country code DE...
VAT number DE814416365 with country code DE is invalid and belongs to --- who lives at ---!
VAT number checked! exiting now...
型
那么,是我在这里做错了什么,还是Web服务真的总是响应“VAT number is invalid”(增值税号码无效)(似乎是这样,因为它用"-“覆盖了名称和地址值)?
1条答案
按热度按时间9ceoxa921#
只要你使用正确的增值税号码(“DE”部分不是增值税号码的一部分),它就能工作!我不知道为什么网络服务不响应错误,而不是给出一个“无效”的信息,但是嘿,它工作了:)