HTTP状态500 - org.springframework.web.client.HttpClientErrorException:404 /

时间:2013-09-05 14:52:18

标签: rest resttemplate

我正在使用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();

请告诉我们是否有人知道。 感谢

1 个答案:

答案 0 :(得分: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();