Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如何解决;在Spring Boot中提取[class com.*”类型的响应时出错?_Json_Rest_Spring Boot_Resttemplate_Http Message Converter - Fatal编程技术网

Json 如何解决;在Spring Boot中提取[class com.*”类型的响应时出错?

Json 如何解决;在Spring Boot中提取[class com.*”类型的响应时出错?,json,rest,spring-boot,resttemplate,http-message-converter,Json,Rest,Spring Boot,Resttemplate,Http Message Converter,您可能想跳到下面的“我的更新2” 我有一个可以工作的RestController,因为当我直接从浏览器访问它时,它会返回JSON响应。但是,当我在不同的有界上下文中从服务发送请求时,我会得到错误: {"timestamp":1579095291446,"message":"Error while extracting response for type [class com.path.to.contexttwo.client.dto.WorkerDetails] and content typ

您可能想跳到下面的“我的更新2”

我有一个可以工作的RestController,因为当我直接从浏览器访问它时,它会返回JSON响应。但是,当我在不同的有界上下文中从服务发送请求时,我会得到错误:

{"timestamp":1579095291446,"message":"Error while extracting response for type 
[class com.path.to.contexttwo.client.dto.WorkerDetails] and content type [application/json]; nested exception is 
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: 
Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false');
nested exception is com.fasterxml.jackson.core.JsonParseException: 
Unexpected character ('<' (code 60)): 
expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n at [Source: (PushbackInputStream); 
line: 1, column: 2]","details":"uri=/context-two/register-new"}
RestClient

package com.path.to.contexttwo.client;

// imports omitted, as well as other code

@Service
public class IAcessRestClientImpl implements IAcessRestClient {

    private final RestTemplate restTemplate;

    @Autowired
    public IAcessRestClientImpl(
            final RestTemplate restTemplate
    ) {
        this.restTemplate = restTemplate;
    }

    @Override
    public WorkerDetails getWorkerDetailsByName(final String userName) throws URISyntaxException {

        Map<String,String> urlVariables = new HashMap<>();
        urlVariables.put("userName", userName);
        return restTemplate.getForObject(
                "http://localhost:8080/iacess/get-worker-details/{userName}",
                WorkerDetails.class,
                urlVariables
        );
    }
}
WorkerDetails也存在于包com.path.to.contextone.ohs_pl中

我已经试了3天了,阅读和调试都没有用。调试器似乎显示错误发生在RestTemplate分析WorkerDetails.class时

我还尝试在所有配置类中使用ComponentScan,因为文件位于单独的包(有界上下文)中,但没有成功

我可以使用调用IAcessRestClient的类中的UserDetailsRepository来获取WorkerDetails,但这会使两个不同的有界上下文依赖于同一个数据库模式

任何帮助都将不胜感激。 我可以发布每个请求的传统代码

提前谢谢

更新 @S B请求输入参数。下面是发送参数的类:

公司服务impl

package com.path.to.contexttwo.domain.services;

// imports

@Service
public class CompanyServiceImpl implements CompanyService {

    private CompanyRepository companyRepository;
    private CompanyWorkerRepositoery companyWorkerRepositoery;
    private WorkerDetailsClient workerDetailsClient;
    private WebApplicationContext applicationContext;

    @Autowired
    CompanyServiceImpl (
            CompanyRepository companyRepository,
            CompanyWorkerRepositoery companyWorkerRepositoery,
            WorkerDetailsClient workerDetailsClient,
            WebApplicationContext applicationContext
    ) {
        this.companyRepository = companyRepository;
        this.companyWorkerRepositoery = companyWorkerRepositoery;
        this.workerDetailsClient = workerDetailsClient;
        this.applicationContext = applicationContext;
    }

    @Transactional
    public Company criateCompany(CompanyDTO dto) throws URISyntaxException { 

        if (dto.getLegalyAuthorized() == true && dto.getTerms() == true) {
            Company company = new Company(
                    dto.getCompanyName(),
                    dto.getStateId()
            );
            company = CompanyRepository.save(company);

            // when saving the company, we also need some details from the current logged in user which can be 
            // retrieved from the idendity and access bounded context. We need those details to be saved in this context            

            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            String name = auth.getName();

            WorkerDetails workerDetails = WorkerDetailsClient.getWorkerDetailsByName(
                    name
            );

            // ... we can't reach the rest of the code anyway, so we omitted            
    }
}
下面是我直接访问RestController时得到的响应:

{"userId":127,"gender":"M","firstName":"Primeiro","lastName":"Último","phoneNumber":"922222222"}
更新2

注释掉了.anyRequest().authenticated()并且所有内容都运行正常!因此,它一直都与Spring安全性有关。真遗憾。现在将尝试在启用安全性的情况下运行。由于重定向到登录页面,我收到了HTML作为响应。正确实现了身份验证(使用基本身份验证的令牌请求)一切都很顺利

谢谢大家!

试试:

return restTemplate.getForObject(
   "http://localhost:8080/iacess/get-worker-details/" + userName,
   WorkerDetails.class);

您是否尝试将{username}替换为用户名的实际值:{username}?请共享您的输入参数和预期响应(当您从浏览器运行请求时,该选项起作用。@JavaBoy,是的,我尝试使用硬编码URI,但得到了相同的错误。实际上,在调试时,url是正确的。@SB,请查看我对问题的更新。谢谢!根据错误,响应中似乎包含一些HTML代码。请检查re中的特殊字符响应数据?谢谢,JavaBoy!实际上,这是我的第一次尝试。我碰巧根据一本书中的示例更改为当前形式。现在将恢复。
package com.path.to.contexttwo.domain.services;

// imports

@Service
public class CompanyServiceImpl implements CompanyService {

    private CompanyRepository companyRepository;
    private CompanyWorkerRepositoery companyWorkerRepositoery;
    private WorkerDetailsClient workerDetailsClient;
    private WebApplicationContext applicationContext;

    @Autowired
    CompanyServiceImpl (
            CompanyRepository companyRepository,
            CompanyWorkerRepositoery companyWorkerRepositoery,
            WorkerDetailsClient workerDetailsClient,
            WebApplicationContext applicationContext
    ) {
        this.companyRepository = companyRepository;
        this.companyWorkerRepositoery = companyWorkerRepositoery;
        this.workerDetailsClient = workerDetailsClient;
        this.applicationContext = applicationContext;
    }

    @Transactional
    public Company criateCompany(CompanyDTO dto) throws URISyntaxException { 

        if (dto.getLegalyAuthorized() == true && dto.getTerms() == true) {
            Company company = new Company(
                    dto.getCompanyName(),
                    dto.getStateId()
            );
            company = CompanyRepository.save(company);

            // when saving the company, we also need some details from the current logged in user which can be 
            // retrieved from the idendity and access bounded context. We need those details to be saved in this context            

            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            String name = auth.getName();

            WorkerDetails workerDetails = WorkerDetailsClient.getWorkerDetailsByName(
                    name
            );

            // ... we can't reach the rest of the code anyway, so we omitted            
    }
}
{"userId":127,"gender":"M","firstName":"Primeiro","lastName":"Último","phoneNumber":"922222222"}
return restTemplate.getForObject(
   "http://localhost:8080/iacess/get-worker-details/" + userName,
   WorkerDetails.class);