2017-12-18 96 views
1

我的api工作正常,并且在POSTMAN中测试时确实返回了JSON结果。我有问题通过使用REST客户端的控制器检索JSON值。我正在使用spring和hibernate。使用REST客户端从JSON获取值

@RequestMapping(value = "/viewForm/{id}") 
    public String viewForm(@Valid @PathVariable("id") String id, Model model) { 

     RestTemplate restTemplate = new RestTemplate(); 

     String fooResourceUrl 
      = "http://localhost:8080/api/delivery/searchDanceform/"+ id; 

     System.out.println(fooResourceUrl); 

     ResponseEntity<DanceApplicationForm> rateResponse = 
       restTemplate.exchange(fooResourceUrl, 
          HttpMethod.GET, null, DanceApplicationForm.class); 
     DanceApplicationForm response = rateResponse.getBody(); 

     System.out.println(response); 
     logger.info("result = {}", response); 

     return VIEW_PATH + "dance-profile"; 

    } 

我得到这个错误,

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/intranet-web] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 404 null] with root cause 
org.springframework.web.client.HttpClientErrorException: 404 null 
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:78) 
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700) 
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653) 
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613) 
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531) 
    at sg.com.ctc.intranet.web.training.controller.DanceController.viewDanceApplicationForm(DanceController.java:239) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 

我自己也尝试这种方式。

String response = restTemplate.getForObject(fooResourceUrl, String.class); 

但仍然无法正常工作。

+0

你调用这个URL里面viewForm控制器的 “http://本地主机:8080/API /交付/ searchDanceform /” + ID;在你的答案部分你的控制器名称是viewDanceApplication,它与调用URL不匹配,这意味着控制器名称必须与searchDanceform相匹配 –

+0

我可以使用REST客户端在我的rest控制器中调用API方法。因此,我认为API链接和控制器名称不必相同。 – Learner

回答

0

你正在通过null值交换的方法,实际上代替null你必须通过实体引用。

HttpHeaders headers = new HttpHeaders(); 
headers.set("Accept", "application/json"); 
HttpEntity entity = new HttpEntity(headers); 
ResponseEntity<DanceApplicationForm> rateResponse = restTemplate.exchange(url, HttpMethod.GET, entity, DanceApplicationForm.class); 
+0

我试过了你的建议,但我仍然得到同样的错误。有没有其他方法可以解决这个问题? – Learner

+0

你可以在交换方法中共享调用url的控制器代码,因为错误是“404”,这基本上是找不到页面或url路径不正确。 –

+0

我有我的控制器在答案部分,因为我不太确定我应该把它放在哪里。请看看。非常感谢! – Learner

0

我把它放在答案部分,因为我不太确定我可以在哪里回复我的代码。

@RequestMapping(value = "/viewDanceApplication/{id}") 
    public String viewDanceApplicationForm(@Valid @PathVariable("id") String id, Model model) { 

     RestTemplate restTemplate = new RestTemplate(); 

     String fooResourceUrl 
      = "http://localhost:8080/api/delivery/searchDanceform/"+ id; 

     HttpHeaders headers = new HttpHeaders(); 
     headers.set("Accept", "application/json"); 
     HttpEntity entity = new HttpEntity(headers); 
     ResponseEntity<DanceApplicationForm> rateResponse = restTemplate.exchange(fooResourceUrl, HttpMethod.GET, entity, DanceApplicationForm.class); 

    logger.info("result = {}", rateResponse); 



    model.addAttribute("showProfile",rateResponse); 

return VIEW_PATH + "dance-profile"; 

    } 
0

这是

处理程序控制器
http://localhost:8080/api/delivery/searchDanceform/ 

@RequestMapping(value = "/searchDanceform/{id}") 
    public String searchDanceform(@Valid @PathVariable("id") String id, Model model) { 
      // do your code and return response 




    }