java - 调用restTemplate.exchange()时出现IllegalStateException

标签 java json file resttemplate

尝试使用RestTemplate.exchange()从json文件读取数据时获取java.lang.IllegalStateException。尝试了很多方法,但都没有成功。任何建议将不胜感激。

代码:

ResponseEntity<GetCustomerResponse> customerResponse = restTemplate.exchange("J:\Backup\FILWORKSPACE\tapp100997_accountopeningservice\target\classes\testdata\CustomerEnquiryApiResponse.json",HttpMethod.POST, request, GetCustomerResponse.class);

异常:

    java.lang.IllegalStateException: Could not create URI object: Illegal character in opaque part at index 2: J:\Backup\FILWORKSPACE\tapp100997_accountopeningservice\target\classes\testdata\CustomerEnquiryApiResponse.json
    at org.springframework.web.util.DefaultUriTemplateHandler.createUri(DefaultUriTemplateHandler.java:168)
    at org.springframework.web.util.DefaultUriTemplateHandler.expandInternal(DefaultUriTemplateHandler.java:105)
    at org.springframework.web.util.AbstractUriTemplateHandler.expand(AbstractUriTemplateHandler.java:106)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:612)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
    at com.fidintl.bs.accountopening.rulengine.rules.CustomerRule.getCustomerDetails(CustomerRule.java:169)
    at com.fidintl.bs.accountopening.rulengine.rules.CustomerRule.execute(CustomerRule.java:55)
    at com.fidintl.bs.accountopening.rulengine.rules.CustomerRuleTest.testCustomerEligibilityWithBlankData(CustomerRuleTest.java:122)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.net.URISyntaxException: Illegal character in opaque part at index 2: J:\Backup\FILWORKSPACE\tapp100997_accountopeningservice\target\classes\testdata\CustomerEnquiryApiResponse.json
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.checkChars(URI.java:3021)
    at java.net.URI$Parser.parse(URI.java:3058)
    at java.net.URI.<init>(URI.java:588)
    at org.springframework.web.util.DefaultUriTemplateHandler.createUri(DefaultUriTemplateHandler.java:165)
    ... 33 more

Json 文件数据:

 {"success":true,"errors":[],"customerDetails":[{"customerReference":{"type":"PARTY_REFERENCE_NUMBER","identifier":"500008713684"},"success":true,"errors":[],"customers":[{"customerType":"INDIVIDUAL_INVESTOR","individualClientDetails":{"personalDetails":{"personName":{"title":"Miss","initials":null,"firstName":"User","lastName":"Payroll","fullName":"Miss User Payroll","printName":"Miss User Payroll"},"dateOfBirth":"1970-12-01","dateOfDeath":null,"gender":"UNKNOWN","maritalStatus":"UNKNOWN","townOfBirth":null,"countryOfBirth":"GB","nationalityDetails":[{"countryCode":"GB","primaryNationalityFlag":null,"reportingNationalityFlag":"Y","reportingDetails":{"nationalIdType":"TAX_IDENTIFIER","nationalId":"AB109999D"}},{"countryCode":"GB","primaryNationalityFlag":"Y","reportingNationalityFlag":null,"reportingDetails":null}],"countriesOfResidence":[{"countryCode":"GB","countryName":"UNITED KINGDOM","primaryCountryOfResidence":"Y"}],"age":"48","niNoIssuedFlag":"Y","anticipatedRetirementAge":null},"crownEmployeeFlag":"N","planParticipantDetails":null,"employerDetails":null,"occupation":null},"institutionalClientDetails":null,"partyReferenceNumber":"500008713684","alternativeReferences":[{"identifierType":"SONATA_CLIENT_ID","identifier":"100668431"},{"identifierType":"INVESTOR_REFERENCE_NUMBER","identifier":"1004664408"}],"lifeCycleStatus":"INVESTOR","inceptionDate":"2019-03-19","dormancyDate":null,"directAdvisedIndicator":"DIRECT_AND_ADVISED","lostContactFlag":"N","probateFlag":"N","courtOfProtectionFlag":"N","languagePreference":"ENG","postalAddresses":[{"contactRole":"CORRESPONDENCE_ADDRESS","nonStructuredAddressDetails":{"line1":"30 Benedict Drive","line2":null,"line3":"FELTHAM","line4":null},"structuredAddressDetails":null,"overrideAddress":"N","administrativeArea":null,"postalTown":"FELTHAM","postalCode":"TW14 8JL","region":"UK","countryCode":"GB","setBy":"500008713684","status":"ACTIVE","validity":{"lastValidatedDate":null,"validFrom":"2019-03-19 18:05:07.000+0000","validTo":null},"countryName":"UNITED KINGDOM"},{"contactRole":"REGISTERED_ADDRESS","nonStructuredAddressDetails":{"line1":"30 Benedict Drive","line2":null,"line3":"FELTHAM","line4":null},"structuredAddressDetails":null,"overrideAddress":"N","administrativeArea":null,"postalTown":"FELTHAM","postalCode":"TW14 8JL","region":"UK","countryCode":"GB","setBy":"500008713684","status":"ACTIVE","validity":{"lastValidatedDate":null,"validFrom":"2019-03-19 18:05:07.000+0000","validTo":null},"countryName":"UNITED KINGDOM"}],"classifications":[{"classificationScheme":"FATCA_US","classificationCode":"INUS05","classificationDescription":"Non US Status"},{"classificationScheme":"OFFICE_CODE","classificationCode":"OAKH","classificationDescription":"FIL Investments International"},{"classificationScheme":"MIFID","classificationCode":"RE","classificationDescription":"Retail"},{"classificationScheme":"GFAS_CUSTOMER_TYPE","classificationCode":"P","classificationDescription":"Private Individual"}],"externalIdentifiers":[{"identifierName":"TAX_IDENTIFICATION_NUMBER","value":"AB109999D","country":"GB","tinNotIssued":"NOT_CONFIRMED"},{"identifierName":"NATIONAL_INSURANCE_NUMBER","value":"AB109999D","country":"GB","tinNotIssued":null}],"salutations":null,"documentDeliveryPreferenceDetails":{"documentDeliveryPreferences":[{"documentType":"COT","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"SAV","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"PLC","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"RRL","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"WPL","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"DTL","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"ITR","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"WL","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"MLC","deliveryMethod":"POST_AND_ONLINE"}],"emailNotificationFlag":"N"},"electronicAddresses":null,"telephones":null,"partyAssociations":null,"amlStatusDetails":null,"bankAccountDetails":null,"w8BenDetails":null,"reportingCurrency":null,"associatedFILEmployee":null,"employmentStatus":null,"marketingSource":null,"partyGenericVariables":{"genericVariables":[{"name":"PLATFORM","values":["SONATA"]},{"name":"PDP_CONSENT","values":["YES"]}]},"restrictions":null,"roleToFIL":null,"marketingPreferences":[],"applicationTerms":[{"applicationTermsAndCondition":{"applicationName":"ISA Declaration","applicationVersion":"1.0","applicationTermsAndConditionsVersion":"2.0","dateAccepted":"2015-03-12 00:00:00.000+0000","applicationType":"DOCUMENT"}},{"applicationTermsAndCondition":{"applicationName":"Doing Business with Fidelity","applicationVersion":"1.0","applicationTermsAndConditionsVersion":"2.0","dateAccepted":"2015-03-12 00:00:00.000+0000","applicationType":"DOCUMENT"}},{"applicationTermsAndCondition":{"applicationName":"Payroll ISA","applicationVersion":"1.0","applicationTermsAndConditionsVersion":"2.0","dateAccepted":"2015-03-12 00:00:00.000+0000","applicationType":"DOCUMENT"}},{"applicationTermsAndCondition":{"applicationName":"Fidelity's Privacy Policy","applicationVersion":"1.0","applicationTermsAndConditionsVersion":"2.0","dateAccepted":"2015-03-12 00:00:00.000+0000","applicationType":"DOCUMENT"}},{"applicationTermsAndCondition":{"applicationName":"US Person","applicationVersion":"1.0","applicationTermsAndConditionsVersion":"2.0","dateAccepted":"2015-03-12 00:00:00.000+0000","applicationType":"DOCUMENT"}},{"applicationTermsAndCondition":{"applicationName":"Client Terms","applicationVersion":"1.0","applicationTermsAndConditionsVersion":"2.0","dateAccepted":"2015-03-12 00:00:00.000+0000","applicationType":"DOCUMENT"}},{"applicationTermsAndCondition":{"applicationName":"Online Terms","applicationVersion":"1.0","applicationTermsAndConditionsVersion":"2.0","dateAccepted":"2015-03-12 00:00:00.000+0000","applicationType":"DOCUMENT"}}],"createdBy":null,"assignedTo":null,"updatedBy":null,"auditDetails":null,"standingPreferences":null,"literaturePreferences":null,"documentDeliveryPreferences":null,"pensionDetails":null,"migration":null,"pensionPlatform":null,"clientRegionDetails":null}]}]}

最佳答案

来自文档 https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html#exchange-java.lang.String-org.springframework.http.HttpMethod-org.springframework.http.HttpEntity-java.lang.Class-java.lang.Object...- ,

Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity.

您需要使用有效的 hhtp 网址。

关于java - 调用restTemplate.exchange()时出现IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57636279/

相关文章:

java - Hibernate - 访问一对多关联的子元素时出现 NullPointerException

JAVA构造函数(我快要用棒球棒砸烂我的电脑了)

json - 如何从 Laravel 中的 json 响应中删除斜杠

php - 使用 json 发送邮件

c# - 使用 .NET 删除目录中超过 3 个月的文件

java - Spring validator : having both annotation and validator implementation

java - NetBeans Profiler 上的 byte[] 和 char[]

json - 有没有一种直接的方法可以将存储在 HDFS 中的数据转换为 JSON?

c - 使用 lseek 函数更改 c 中的文件位置后损坏的文件(大小相同但内容损坏)

c - 在目录的每个文件中追加一行