Java Code Examples for org.springframework.http.HttpEntity#getBody()
The following examples show how to use
org.springframework.http.HttpEntity#getBody() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HomeController.java From jVoiD with Apache License 2.0 | 8 votes |
@RequestMapping("/order") public @ResponseBody String orderProductNowById(@RequestParam("cartId") String cartId, @RequestParam("prodId") String productId) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); JSONObject jsonObj = new JSONObject(); try { jsonObj.put("cartId", Integer.parseInt(cartId)); jsonObj.put("productId", Integer.parseInt(productId)); jsonObj.put("attributeId", 1); jsonObj.put("quantity", 1); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("param jsonObj=>"+jsonObj.toString()); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(ServerUris.QUOTE_SERVER_URI+URIConstants.ADD_PRODUCT_TO_CART) .queryParam("params", jsonObj); HttpEntity<?> entity = new HttpEntity<>(headers); HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity, String.class); return returnString.getBody(); }
Example 2
Source File: HomeController.java From jVoiD with Apache License 2.0 | 6 votes |
@RequestMapping("/cart") public @ResponseBody String getCartById(@RequestParam("cartId") String cartId) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); JSONObject jsonObj = new JSONObject(); try { jsonObj.put("cartId", Integer.parseInt(cartId)); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("param jsonObj=>"+jsonObj.toString()); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(ServerUris.QUOTE_SERVER_URI+URIConstants.GET_CART) .queryParam("params", jsonObj); HttpEntity<?> entity = new HttpEntity<>(headers); HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity, String.class); return returnString.getBody(); }
Example 3
Source File: FormHttpMessageConverter.java From lams with GNU General Public License v2.0 | 6 votes |
@SuppressWarnings("unchecked") private void writePart(String name, HttpEntity<?> partEntity, OutputStream os) throws IOException { Object partBody = partEntity.getBody(); Class<?> partType = partBody.getClass(); HttpHeaders partHeaders = partEntity.getHeaders(); MediaType partContentType = partHeaders.getContentType(); for (HttpMessageConverter<?> messageConverter : this.partConverters) { if (messageConverter.canWrite(partType, partContentType)) { HttpOutputMessage multipartMessage = new MultipartHttpOutputMessage(os); multipartMessage.getHeaders().setContentDispositionFormData(name, getFilename(partBody)); if (!partHeaders.isEmpty()) { multipartMessage.getHeaders().putAll(partHeaders); } ((HttpMessageConverter<Object>) messageConverter).write(partBody, partContentType, multipartMessage); return; } } throw new HttpMessageNotWritableException("Could not write request: no suitable HttpMessageConverter " + "found for request type [" + partType.getName() + "]"); }
Example 4
Source File: FormHttpMessageConverter.java From java-technology-stack with MIT License | 6 votes |
@SuppressWarnings("unchecked") private void writePart(String name, HttpEntity<?> partEntity, OutputStream os) throws IOException { Object partBody = partEntity.getBody(); if (partBody == null) { throw new IllegalStateException("Empty body for part '" + name + "': " + partEntity); } Class<?> partType = partBody.getClass(); HttpHeaders partHeaders = partEntity.getHeaders(); MediaType partContentType = partHeaders.getContentType(); for (HttpMessageConverter<?> messageConverter : this.partConverters) { if (messageConverter.canWrite(partType, partContentType)) { Charset charset = isFilenameCharsetSet() ? StandardCharsets.US_ASCII : this.charset; HttpOutputMessage multipartMessage = new MultipartHttpOutputMessage(os, charset); multipartMessage.getHeaders().setContentDispositionFormData(name, getFilename(partBody)); if (!partHeaders.isEmpty()) { multipartMessage.getHeaders().putAll(partHeaders); } ((HttpMessageConverter<Object>) messageConverter).write(partBody, partContentType, multipartMessage); return; } } throw new HttpMessageNotWritableException("Could not write request: no suitable HttpMessageConverter " + "found for request type [" + partType.getName() + "]"); }
Example 5
Source File: FormHttpMessageConverter.java From onetwo with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") private void writePart(String name, HttpEntity<?> partEntity, OutputStream os) throws IOException { Object partBody = partEntity.getBody(); if (partBody == null) { throw new IllegalStateException("Empty body for part '" + name + "': " + partEntity); } Class<?> partType = partBody.getClass(); HttpHeaders partHeaders = partEntity.getHeaders(); MediaType partContentType = partHeaders.getContentType(); for (HttpMessageConverter<?> messageConverter : this.partConverters) { if (messageConverter.canWrite(partType, partContentType)) { Charset charset = isFilenameCharsetSet() ? StandardCharsets.US_ASCII : this.charset; HttpOutputMessage multipartMessage = new MultipartHttpOutputMessage(os, charset); multipartMessage.getHeaders().setContentDispositionFormData(name, getFilename(partBody)); if (!partHeaders.isEmpty()) { multipartMessage.getHeaders().putAll(partHeaders); } ((HttpMessageConverter<Object>) messageConverter).write(partBody, partContentType, multipartMessage); return; } } throw new HttpMessageNotWritableException("Could not write request: no suitable HttpMessageConverter " + "found for request type [" + partType.getName() + "]"); }
Example 6
Source File: FormHttpMessageConverter.java From spring-analysis-note with MIT License | 6 votes |
@SuppressWarnings("unchecked") private void writePart(String name, HttpEntity<?> partEntity, OutputStream os) throws IOException { Object partBody = partEntity.getBody(); if (partBody == null) { throw new IllegalStateException("Empty body for part '" + name + "': " + partEntity); } Class<?> partType = partBody.getClass(); HttpHeaders partHeaders = partEntity.getHeaders(); MediaType partContentType = partHeaders.getContentType(); for (HttpMessageConverter<?> messageConverter : this.partConverters) { if (messageConverter.canWrite(partType, partContentType)) { Charset charset = isFilenameCharsetSet() ? StandardCharsets.US_ASCII : this.charset; HttpOutputMessage multipartMessage = new MultipartHttpOutputMessage(os, charset); multipartMessage.getHeaders().setContentDispositionFormData(name, getFilename(partBody)); if (!partHeaders.isEmpty()) { multipartMessage.getHeaders().putAll(partHeaders); } ((HttpMessageConverter<Object>) messageConverter).write(partBody, partContentType, multipartMessage); return; } } throw new HttpMessageNotWritableException("Could not write request: no suitable HttpMessageConverter " + "found for request type [" + partType.getName() + "]"); }
Example 7
Source File: AuthenticationStepsOperator.java From spring-vault with Apache License 2.0 | 6 votes |
private Mono<Object> doHttpRequest(HttpRequestNode<Object> step, Object state) { HttpRequest<Object> definition = step.getDefinition(); HttpEntity<?> entity = getEntity(definition.getEntity(), state); RequestBodySpec spec; if (definition.getUri() == null) { spec = this.webClient.method(definition.getMethod()).uri(definition.getUriTemplate(), definition.getUrlVariables()); } else { spec = this.webClient.method(definition.getMethod()).uri(definition.getUri()); } for (Entry<String, List<String>> header : entity.getHeaders().entrySet()) { spec = spec.header(header.getKey(), header.getValue().get(0)); } if (entity.getBody() != null && !entity.getBody().equals(Undefinded.INSTANCE)) { return spec.bodyValue(entity.getBody()).retrieve().bodyToMono(definition.getResponseType()); } return spec.retrieve().bodyToMono(definition.getResponseType()); }
Example 8
Source File: HttpEntityMethodProcessor.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { mavContainer.setRequestHandled(true); if (returnValue == null) { return; } ServletServerHttpRequest inputMessage = createInputMessage(webRequest); ServletServerHttpResponse outputMessage = createOutputMessage(webRequest); Assert.isInstanceOf(HttpEntity.class, returnValue); HttpEntity<?> responseEntity = (HttpEntity<?>) returnValue; HttpHeaders entityHeaders = responseEntity.getHeaders(); if (!entityHeaders.isEmpty()) { outputMessage.getHeaders().putAll(entityHeaders); } Object body = responseEntity.getBody(); if (responseEntity instanceof ResponseEntity) { outputMessage.setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode()); if (HttpMethod.GET == inputMessage.getMethod() && isResourceNotModified(inputMessage, outputMessage)) { outputMessage.setStatusCode(HttpStatus.NOT_MODIFIED); // Ensure headers are flushed, no body should be written. outputMessage.flush(); // Skip call to converters, as they may update the body. return; } } // Try even with null body. ResponseBodyAdvice could get involved. writeWithMessageConverters(body, returnType, inputMessage, outputMessage); // Ensure headers are flushed even if no body was written. outputMessage.flush(); }
Example 9
Source File: ServletAnnotationControllerHandlerMethodTests.java From java-technology-stack with MIT License | 5 votes |
@PostMapping("/foo") public ResponseEntity<String> foo(HttpEntity<byte[]> requestEntity) throws Exception { assertNotNull(requestEntity); assertEquals("MyValue", requestEntity.getHeaders().getFirst("MyRequestHeader")); String body = new String(requestEntity.getBody(), "UTF-8"); assertEquals("Hello World", body); URI location = new URI("/foo"); return ResponseEntity.created(location).header("MyResponseHeader", "MyValue").body(body); }
Example 10
Source File: ServletAnnotationControllerHandlerMethodTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@RequestMapping("/foo") public ResponseEntity<String> foo(HttpEntity<byte[]> requestEntity) throws UnsupportedEncodingException { assertNotNull(requestEntity); assertEquals("MyValue", requestEntity.getHeaders().getFirst("MyRequestHeader")); String requestBody = new String(requestEntity.getBody(), "UTF-8"); assertEquals("Hello World", requestBody); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.set("MyResponseHeader", "MyValue"); return new ResponseEntity<String>(requestBody, responseHeaders, HttpStatus.CREATED); }
Example 11
Source File: LibraryController.java From steady with Apache License 2.0 | 5 votes |
@RequestMapping(value = "/{digest}/upload", method = RequestMethod.POST, consumes = {"application/octet-stream"}) @JsonView(Views.LibDetails.class) ResponseEntity<Library> postLibraryJAR(@PathVariable String digest, HttpEntity<byte[]> requestEntity) { LibraryController.log.info("Called postLibrary for JAR [" +digest+ "]"); try { byte[] payload = requestEntity.getBody(); InputStream inputStream = new ByteArrayInputStream(payload); if(!Files.exists(Paths.get(VulasConfiguration.getGlobal().getLocalM2Repository().toString()+ File.separator +"uknownJars" ))){ Files.createDirectories(Paths.get(VulasConfiguration.getGlobal().getLocalM2Repository().toString()+ File.separator +"uknownJars" )); } String saveFilePath = VulasConfiguration.getGlobal().getLocalM2Repository().toString()+ File.separator +"uknownJars" + File.separator + digest + ".jar"; LibraryController.log.info("Saving JAR [" +digest+ "] to file [" + saveFilePath+"]"); // opens an output stream to save into file FileOutputStream outputStream = new FileOutputStream(saveFilePath); int bytesRead = -1; byte[] buffer = new byte[inputStream.available()]; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.close(); inputStream.close(); return new ResponseEntity<Library>(HttpStatus.OK); } catch (IOException e) { LibraryController.log.error("Error while saving the received JAR file [" + e + "]"); return new ResponseEntity<Library>(HttpStatus.INTERNAL_SERVER_ERROR); } }
Example 12
Source File: AuthenticationStepsExecutor.java From spring-vault with Apache License 2.0 | 5 votes |
private static HttpEntity<?> getEntity(HttpEntity<?> entity, @Nullable Object state) { if (entity == null) { return state == null ? HttpEntity.EMPTY : new HttpEntity<>(state); } if (entity.getBody() == null && state != null) { return new HttpEntity<>(state, entity.getHeaders()); } return entity; }
Example 13
Source File: UserAuthenticationProvider.java From jVoiD with Apache License 2.0 | 5 votes |
@Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // Make an API Call to the Customer WAR to get the user data based on this email address. //JSONObject user = userservice.getCustomerByEmail(username); RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(ServerUris.CUSTOMER_SERVER_URI+URIConstants.GET_CUSTOMER_BY_EMAIL) .queryParam("params", "{email: " + username + "}"); HttpEntity<?> entity = new HttpEntity<>(headers); HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity, String.class); SerializableJSONObject user = null; try { JSONObject temp = new JSONObject(returnString.getBody()); user = new SerializableJSONObject(temp); System.out.println("User: " + user.toString()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(user == null) { throw new UsernameNotFoundException(String.format("User %s not exist!", username)); } return new UserRepositoryUserDetails(user); }
Example 14
Source File: RESTClientTest.java From secure-data-service with Apache License 2.0 | 4 votes |
@Override public Object answer(InvocationOnMock invocation) throws Throwable { HttpEntity httpEntity = (HttpEntity) invocation.getArguments()[1]; json = (String) httpEntity.getBody(); return null; }
Example 15
Source File: RequestDataController.java From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International | 4 votes |
@RequestMapping(value = "entity", method = RequestMethod.POST) public @ResponseBody String withEntity(HttpEntity<String> entity) { return "Posted request body '" + entity.getBody() + "'; headers = " + entity.getHeaders(); }
Example 16
Source File: JacksonHintsIntegrationTests.java From java-technology-stack with MIT License | 4 votes |
@PostMapping("/request/entity/mono") public Mono<JacksonViewBean> entityMonoRequest(@JsonView(MyJacksonView1.class) HttpEntity<Mono<JacksonViewBean>> entityMono) { return entityMono.getBody(); }
Example 17
Source File: JacksonHintsIntegrationTests.java From java-technology-stack with MIT License | 4 votes |
@PostMapping("/request/entity/flux") public Flux<JacksonViewBean> entityFluxRequest(@JsonView(MyJacksonView1.class) HttpEntity<Flux<JacksonViewBean>> entityFlux) { return entityFlux.getBody(); }
Example 18
Source File: JacksonHintsIntegrationTests.java From spring-analysis-note with MIT License | 4 votes |
@PostMapping("/request/entity/mono") public Mono<JacksonViewBean> entityMonoRequest(@JsonView(MyJacksonView1.class) HttpEntity<Mono<JacksonViewBean>> entityMono) { return entityMono.getBody(); }
Example 19
Source File: RequestResponseBodyMethodProcessorTests.java From java-technology-stack with MIT License | 4 votes |
@RequestMapping @ResponseBody public JacksonViewBean handleHttpEntity(@JsonView(MyJacksonView1.class) HttpEntity<JacksonViewBean> entity) { return entity.getBody(); }
Example 20
Source File: RequestResponseBodyMethodProcessorTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@RequestMapping @ResponseBody public JacksonViewBean handleHttpEntity(@JsonView(MyJacksonView1.class) HttpEntity<JacksonViewBean> entity) { return entity.getBody(); }