用于清理Web服务的RestTemplate响应类型问题

时间:2017-08-22 12:09:08

标签: java spring rest

我写过以下REST Web服务。

现在我想使用RestTemplate来使用这个Web服务。但是我不确定restTemplate.exchange(?)应该返回什么类型。你能帮助什么应该返回类型吗?

    @RequestMapping(value = "/customercontact/transid/{brokerid}", method = RequestMethod.GET)
        public ResponseEntity<HashMap<String, String>> getTransactionIdWithPublicKey(@PathVariable("brokerid") String brokerid){
         HashMap<String, String> transactionid = customerService.getTransactionid(brokerid);
            if(transactionid != null)
                return new ResponseEntity<HashMap<String, String>>(transactionid,HttpStatus.OK);
            else 
                return new ResponseEntity<HashMap<String, String>>(HttpStatus.NOT_FOUND);

        }
public void doGet( HttpServletRequest request,
                          HttpServletResponse response ) throws IOException
       {

 RestTemplate restTemplate = new RestTemplate();
          HttpHeaders requestHeaders = new HttpHeaders();
          requestHeaders.add("SM_USER", "wbrokere");
          MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();   
          HttpEntity<?> httpEntity = new HttpEntity<Object>(body, requestHeaders);
          ResponseEntity<String> responseobj = restTemplate.exchange("http://127.0.0.1:24000/webbroker/getusername", HttpMethod.GET, httpEntity,?);
    }

1 个答案:

答案 0 :(得分:0)

您的Rest Service HashMap.class

中使用的相同类型
ResponseEntity<HashMap> responseobj = `restTemplate.exchange("http://127.0.0.1:24000/webbroker/getusername", HttpMethod.GET, httpEntity,HashMap.class);`
responseobj.getBody(); //To get the body of the response entity

在POM.xml中添加Jakson依赖项

<!-- Jackson for Rest Controller -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.8.8</version>
        </dependency>