使用REST客户端从JSON获取值

问题描述 投票:1回答:3

我的api工作正常,我在POSTMAN中测试时确实得到了JSON结果。我在使用REST客户端通过控制器检索JSON值时遇到问题。我正在使用spring和hibernate。

@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);

但仍然无法正常工作。

spring rest
3个回答
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




    }
© www.soinside.com 2019 - 2024. All rights reserved.