本文整理汇总了Java中org.springframework.http.HttpHeaders.setContentType方法的典型用法代码示例。如果您正苦于以下问题:Java HttpHeaders.setContentType方法的具体用法?Java HttpHeaders.setContentType怎么用?Java HttpHeaders.setContentType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.http.HttpHeaders
的用法示例。
在下文中一共展示了HttpHeaders.setContentType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doPost
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
/**
* 执行POST请求
*
* @param url
* @param contentType
* @param requestBody
* @return
*/
protected WebhookRequestResponse doPost(String url, String contentType, Object requestBody) {
// 请求头
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.parseMediaType(contentType));
HttpEntity<?> requestEntity = new HttpEntity(requestBody, requestHeaders);
try {
// 执行请求
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);
// 返回响应结果
return new WebhookRequestResponse(requestHeaders, requestBody, responseEntity);
} catch (Exception e) {
return new WebhookRequestResponse(requestHeaders, requestBody, e);
}
}
示例2: executeAcademicSessionAPIs
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
/**This method executes following apis
*<code>/api/auth/login</code>
*<code>/api/classes/{classId}/results<code>
*<code>/api/academicsessions</code>
*<code>/api/academicsessions/{academicsessionId}</code>
*<code>/api/classes/{classId}/lineitems/{lineitemid}/results</code>
*<code>/api/classes/{classId}/results
*<code>/api/users/{userId}/results</code>
* @throws IOException
* @throws URISyntaxException
*/
@Test
public void executeAcademicSessionAPIs() throws IOException, URISyntaxException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("X-Requested-With", "XMLHttpRequest");
headers.add("Cache-Control", "no-cache");
LoginRequest request = new LoginRequest(username,password);
String requestbody = json(request);
HttpEntity<Object> entity = new HttpEntity<Object>(requestbody,headers);
Token auth=
restTemplate.postForObject("/api/auth/login", entity, Token.class);
token = auth.token;
// String sourcedId = executeSaveClassAPI();
String resultSourcedId = saveResultForClass(TestData.CLASS_SOURCED_ID);
executeGetResultForLineitemAPI();
executeGetResultForClassAPI();
executeGetResultForUserAPI();
String sourcedId = saveAcademicSession(token);
getAcademicSession(sourcedId);
}
示例3: doPost
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
private static void doPost(String deviceTypeId, String token) throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", token);
headers.setContentType(MediaType.APPLICATION_JSON);
String postJson = new String(Files.readAllBytes(Paths.get(String.format("src/test/resources/%s.json", deviceTypeId))));
new RestTemplate().exchange(String.format(GATEWAY_URL, deviceTypeId), HttpMethod.POST, new HttpEntity<>(postJson, headers), String.class);
}
示例4: checkoutOrder
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
@RequestMapping("/cart-checkout")
public ShoppingCart checkoutOrder() {
LOG.info("you called home");
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<ShoppingCart> cart = new HttpEntity<>((new ShoppingCart(1, Arrays.asList(new LineItem(1, "abc")))),
headers);
ShoppingCart response = restTemplate.postForObject("http://localhost:9000/checkout", cart, ShoppingCart.class);
return response;
}
示例5: handleInvalidServiceRequest
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
@ExceptionHandler({InvalidServiceException.class})
protected ResponseEntity<Object> handleInvalidServiceRequest(RuntimeException e,
WebRequest request) {
logWarning(request, e);
ErrorResource error = new ErrorResource("[Internal Server Error]", e.getMessage());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return handleExceptionInternal(e, error, headers, HttpStatus.INTERNAL_SERVER_ERROR, request);
}
示例6: getHeaders
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
private HttpHeaders getHeaders() {
HttpHeaders headers1 = new HttpHeaders();
headers1.setContentType(MediaType.APPLICATION_JSON);
headers1.add("X-Requested-With", "XMLHttpRequest");
headers1.add("Cache-Control", "no-cache");
headers1.set("Authorization", "Bearer "+ token);
return headers1;
}
示例7: getHttpHeaders
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
public static HttpHeaders getHttpHeaders(UriComponentsBuilder uriComponentsBuilder,String uri,Object... uriVariableValues){
UriComponents uriComponents = uriComponentsBuilder.path(uri).buildAndExpand(uriVariableValues);
HttpHeaders headers = new HttpHeaders();
try {
headers.setLocation(new URI(uriComponents.getPath()));
}catch (Exception e){
logger.error(e.getStackTrace().toString());
}
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
示例8: loginRequest
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
private HttpEntity<MultiValueMap<String, String>> loginRequest() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("username", "jordan");
map.add("password", "password");
return new HttpEntity<>(map, headers);
}
示例9: getProcessDefinitionXml
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
@RequestMapping(value = "/process-definition/{processDefinitionId}/xml", method = RequestMethod.GET, name="流程定义XML")
public ResponseEntity<byte[]> getProcessDefinitionXml(@PathVariable String processDefinitionId) {
ProcessDefinition processDefinition = getProcessDefinitionFromRequest(processDefinitionId);
String deploymentId = processDefinition.getDeploymentId();
String resourceId = processDefinition.getResourceName();
if (deploymentId == null) {
throw new FlowableIllegalArgumentException("No deployment id provided");
}
if (resourceId == null) {
throw new FlowableIllegalArgumentException("No resource id provided");
}
Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(deploymentId).singleResult();
if (deployment == null) {
throw new FlowableObjectNotFoundException("Could not find a deployment with id '" + deploymentId + "'.", Deployment.class);
}
List<String> resourceList = repositoryService.getDeploymentResourceNames(deploymentId);
if (resourceList.contains(resourceId)) {
final InputStream resourceStream = repositoryService.getResourceAsStream(deploymentId, resourceId);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.TEXT_XML);
try {
return new ResponseEntity<byte[]>(IOUtils.toByteArray(resourceStream), responseHeaders,HttpStatus.OK);
} catch (Exception e) {
throw new FlowableException("Error converting resource stream", e);
}
} else {
throw new FlowableObjectNotFoundException("Could not find a resource with id '" + resourceId + "' in deployment '" + deploymentId + "'.", String.class);
}
}
示例10: generatePartialResourceRequest
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
private HttpEntity<MultiValueMap<String, ?>> generatePartialResourceRequest(UploadApplicationPayload application,
CloudResources knownRemoteResources) throws IOException {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<String, Object>(2);
body.add("application", application);
ObjectMapper mapper = new ObjectMapper();
String knownRemoteResourcesPayload = mapper.writeValueAsString(knownRemoteResources);
body.add("resources", knownRemoteResourcesPayload);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
return new HttpEntity<MultiValueMap<String, ?>>(body, headers);
}
示例11: request
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
private HttpEntity<Object> request(Map<String, Map<String, String>> params) {
HttpHeaders headers = new HttpHeaders();
if (params.containsKey(SagaRequest.PARAM_JSON)) {
headers.setContentType(APPLICATION_JSON);
return new HttpEntity<>(params.get(SagaRequest.PARAM_JSON).get(SagaRequest.PARAM_JSON_BODY), headers);
}
if (params.containsKey(SagaRequest.PARAM_FORM)) {
headers.setContentType(APPLICATION_FORM_URLENCODED);
return new HttpEntity<>(params.get(SagaRequest.PARAM_FORM), headers);
}
return null;
}
示例12: init
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Initialize a {@code SmsService} instance.
*
* @param id The LC id
* @param key The LC key
*/
public void init(String id, String key) {
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
converter.setWriteAcceptCharset(false);
restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(0, converter);
mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
headers = new HttpHeaders();
headers.add("X-LC-Id", id);
headers.add("X-LC-Key", key);
headers.setContentType(MediaType.APPLICATION_JSON);
}
示例13: fallbackResponse
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
@Override
public ClientHttpResponse fallbackResponse() {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return this.getStatusCode().value();
}
@Override
public String getStatusText() throws IOException {
return this.getStatusCode().getReasonPhrase();
}
@Override
public void close() {
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("Customer service is not available,please try later.".getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
示例14: getsFlightsWithUnderlyingService
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
@Test
public void getsFlightsWithUnderlyingService() {
when(flightService.getFlightByAirportsAndDate(
departFlight.getFlightSegment().getOriginPort(),
departFlight.getFlightSegment().getDestPort(),
departFlight.getScheduledDepartureTime())).thenReturn(singletonList(departFlight));
when(flightService.getFlightByAirportsAndDate(
departFlight.getFlightSegment().getDestPort(),
departFlight.getFlightSegment().getOriginPort(),
returnFlight.getScheduledDepartureTime())).thenReturn(singletonList(returnFlight));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.add("fromAirport", departFlight.getFlightSegment().getOriginPort());
form.add("toAirport", departFlight.getFlightSegment().getDestPort());
form.add("fromDate", isoDateTime(departFlight.getScheduledDepartureTime()));
form.add("returnDate", isoDateTime(returnFlight.getScheduledDepartureTime()));
form.add("oneWay", String.valueOf(false));
ResponseEntity<TripFlightOptions> responseEntity = restTemplate.exchange(
"/rest/api/flights/queryflights",
HttpMethod.POST,
new HttpEntity<>(form, headers),
TripFlightOptions.class
);
assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
TripFlightOptions flightOptions = responseEntity.getBody();
assertThat(flightOptions.getTripLegs(), is(2));
List<TripLegInfo> tripFlights = flightOptions.getTripFlights();
assertThat(tripFlights.get(0).getFlightsOptions(), contains(toFlightInfo(departFlight)));
assertThat(tripFlights.get(1).getFlightsOptions(), contains(toFlightInfo(returnFlight)));
}
示例15: prepareHeaders
import org.springframework.http.HttpHeaders; //导入方法依赖的package包/类
private HttpHeaders prepareHeaders(StrTup... stringTuples)
{
HttpHeaders result = new HttpHeaders();
if (stringTuples != null) {
for (StrTup t : stringTuples) {
result.add(t.getKey(), t.getValue());
}
}
result.setContentType(MediaType.APPLICATION_JSON);
return result;
}