我正在尝试向另一个spring应用发出发布请求,但出现404错误。
UriComponentsBuilder builder = UriComponentsBuilder
.fromUriString(config.getUrl() +
"jobs/create_job");
log.info("Sending job to api at " + builder.toUriString());
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<EnrichmentJob> entity = new HttpEntity<>(job, headers);
return restTemplate.exchange(builder.toUriString(), HttpMethod.POST,
entity, Job.class);
} catch (HttpServerErrorException ex) {
log.info("Error creating job " + ex.getResponseBodyAsString());
throw new EnrichmentException(ex.getResponseBodyAsString(), ex.getStatusCode());
} catch (ResourceAccessException ex) {
log.info("Error creating job " + ex.getMessage());
throw new EnrichmentException(ex.getMessage());
} catch (HttpClientErrorException ex) {
log.info("Error creating job " + ex.getMessage());
throw new EnrichmentException(ex.getMessage(), ex.getStatusCode());
}
2019-01-30 18:46:58.002 INFO 25897 --- [nio-8082-exec-5] c.t.m.c.repository.EnrichmentRepository : Sending job to api at http://localhost:8080/v1/enrichment_jobs/create_job
2019-01-30 18:46:58.008 INFO 25897 --- [nio-8082-exec-5] c.t.m.c.repository.EnrichmentRepository : Error creating job 404 Not Found
2019-01-30 18:46:58.009 INFO 25897 --- [nio-8082-exec-5] c.t.m.c.l.ApplicationEventListener : Event ServletRequestHandledEvent: url=[/enrichment_jobs/v1/create_job]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[dispatcherServlet]; session=[null]; user=[null]; time=[9ms]; status=[OK]
对此端点发出请求
@PostMapping(path = "/create_job",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Jobs> createJob(
@RequestBody JobsRequest enrichmentJob)
throws DuplicateJobException {
Jobs job = service.convertToJobs(enrichmentJob);
try {
return new ResponseEntity<>(
service.createJob(job), HttpStatus.OK);
} catch (DuplicateJobException ex) {
return new ResponseEntity<>(job, HttpStatus.OK);
}
}
记录url显示该URL正确,并且我可以在邮递员中成功使用该端点。我需要设置其他标题吗?
使用邮递员使用相同的网址:
http://localhost:8080/v1/enrichment_jobs/create_job
带有请求正文:
{
"status": "IN_PROCESS",
"host_name": "host",
"job_type": "UPLOAD",
"job_id": "3934582d-ed3e-4dr5-86b6-ad17f9ed7543",
"asset_id": "3934582d-ed3e-4dr5-86b6-ad17f9ed7315"
}
按预期创建并返回资源。
职位类别:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class JobsRequest {
private String jobId;
private String assetId;
private String status;
private String jobType;
private String hostName;
private String jobStatus;
private String createdBy;
private String lastUpdatedBy;
}