【问题标题】:HTTP Status 500 - org.springframework.web.client.HttpClientErrorException: 404 /HTTP 状态 500 - org.springframework.web.client.HttpClientErrorException: 404 /
【发布时间】:2013-09-05 14:52:18
【问题描述】:

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

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

【问题讨论】:

  • 我缺少 web.xml。可以加吗?

标签: rest resttemplate


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-01-01
    • 2014-01-08
    • 2018-03-05
    • 1970-01-01
    • 2012-12-04
    • 2017-11-01
    • 2016-05-29
    • 2015-05-22
    • 1970-01-01
    相关资源
    最近更新 更多