0

我正在使用 RestTemplate,但是当我调用 postFor 函数时,我得到以下异常,下面是显示细节的代码:

控制器

@Controller
@RequestMapping("/data")
public class DataController {

    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public ResponseEntity<ManagementResource> postData(@PathVariable("id") String id,@RequestBody Data1 data) {
        RSResponse<Data1> response = new RSResponse<Data1>();
        response.setStatus(RSResponse.Status.SUCCESS);
        response.setData(data);
        return new ResponseEntity<ManagementResource>(HttpStatus.CREATED);
    }
}

客户端代码:

RestTemplate rt = new RestTemplate();
        Data1 d = new Data1();

        d.setEmp_id(1);
        d.setEmp_name("abc");
        d.setEmp_salary(10000);

        Map<String, String> vars = new HashMap<String, String>();
        vars.put("id", "JS01");
        String url = "http://localhost:8090/JSFFaceletsTutorial/data/{id}";

        ResponseEntity<Data1> response = rt.postForEntity(url,d,Data1.class,vars);
        Data1 data = response.getBody();

请告诉是否有人知道。谢谢

4

1 回答 1

0

您的服务是否需要标头?如果是这样,你可以这样通过,

MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Accept", "application/json");
HttpEntity<Data1> request = new HttpEntity<Data1>(d, headers);

ResponseEntity<Data1> response = rt.postForEntity(url,request,Data1.class,vars);
Data1 data = response.getBody();
于 2013-09-11T13:07:35.607 回答