resttemplate

415 Unsupported Media Type while sending json file over REST Template

白昼怎懂夜的黑 提交于 2019-12-02 03:52:44
I am trying to send a json file over REST Template. When I send it via POST man as MULTIPART_FORM_DATA, it works fine. The name I am supposed to give is specific (lets say aaa ). Attached screenshot of POSTMAN. But when I try same in code as specified in another stackoverflow post , I get 415 Unsupported Media Type error as org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) ~[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE] at org.springframework.web.client

How to access a huge JSON coming (from a spring RESTful Service) in a spring MVC app using RestTemplate

荒凉一梦 提交于 2019-12-02 03:31:45
My Spring RESTful web service is returning a JSON form of- [{"key1":"value1","key2":"value2","key3":"value3"},{"key4":"value4","key5":"value5","key6":"value6"}] Now when my spring MVC app, try to access it, to show in a JSP then Exception occurs saying- no suitable HttpMessageConverter found Please help me where I going wrong.Here is my code- Inside @Controller class of my spring MVC app calling the RESTful service //**com.songs.controllers.FrontSongController.java** </* author Rohit Tiwari */> @RequestMapping(value="/alls",method=RequestMethod.POST) public String getAllSongs(ModelMap md) {

Springboot — 用更优雅的方式发HTTP请求(RestTemplate详解)

荒凉一梦 提交于 2019-12-02 03:24:07
RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。 我之前的HTTP开发是用apache的HttpClient开发,代码复杂,还得操心资源回收等。代码很复杂,冗余代码多,稍微截个图,这是我封装好的一个post请求工具: 本教程将带领大家实现Spring生态内RestTemplate的Get请求和Post请求还有exchange指定请求类型的实践和RestTemplate核心方法源码的分析,看完你就会用优雅的方式来发HTTP请求。 1.简述RestTemplate 是Spring用于同步client端的核心类,简化了与http服务的通信,并满足RestFul原则,程序代码可以给它提供URL,并提取结果。默认情况下,RestTemplate默认依赖jdk的HTTP连接工具。当然你也可以 通过setRequestFactory属性切换到不同的HTTP源,比如Apache HttpComponents、Netty和OkHttp。 RestTemplate能大幅简化了提交表单数据的难度,并且附带了自动转换JSON数据的功能,但只有理解了HttpEntity的组成结构(header与body),且理解了与uriVariables之间的差异,才能真正掌握其用法

Generated Swagger REST client does not handle + character correctly for query parameter

流过昼夜 提交于 2019-12-02 02:12:17
I have this Spring REST controller method: @ApiOperation("My method") @RequestMapping(method = RequestMethod.POST, value = "/myMethod") public void myMethod(@RequestParam("myParam") String myParam) { ... } The REST client is generated using swagger codegen CLI with language Java and library resttemplate : public void myMethod(String myParam) throws RestClientException { ... return apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType); } And the source code for ApiClient#invokeAPI - which is also generated - is:

How to use query parameter represented as JSON with Spring RestTemplate?

江枫思渺然 提交于 2019-12-01 23:56:40
I need to make a request to an HTTP endpoint having a query parameter represented as JSON using Spring RestTemplate. restTemplate.getForObject( apiRoot + "/path" + "?object={myObject}", Response.class, new MyObject()) Here I need MyObject to be converted to JSON (and URL-encoded obviously). But RestTemplate just converts it to String with toString call instead. MyObject is convertable to JSON by Jackson. UriComponentsBuilder behaves the same way: UriComponentsBuilder.fromHttpUrl(apiRoot) .path("/path") .queryParam("object", new MyObject())) .queryParam("access_token", accessToken) .toUri() Is

微服务架构

ε祈祈猫儿з 提交于 2019-12-01 23:37:02
                                微服务架构 一、微服务定义: 1、分布式服务组成的系统 2、按照业务,而不是技术来划分组织 3、做有生命的产品而不是项目 5、自动化运维( DevOps ) 6、高度容错性 ...... 二、搭建项目   以上是项目搭建目录,分别是eureka-server、spring-servic-9090、spring-servic-9091、spring-servic-9092。 eureka-server目录: 1、新建EurekaApplication作为启动Eureka服务。 package appeurkaservic; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/21 */

Spring Cloud Alibaba学习笔记(4) - Feign

大兔子大兔子 提交于 2019-12-01 23:00:51
什么是Feign Feign是一个声明式Web Service客户端。 使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。 Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。 Feign的组成 接口 作用 默认值 Feign.Builder Feign的入口 Feign.Builder Client Feign底层用什么去请求 和Ribbon配合时: LoadBalancerFeignClient(代理模式,可以为Feign引入连接池) 不和Ribbon配合时: Fgien.Client.Default(URLConnection,没有连接池,没有资源管理,性能较差) Contract 契约,注解支持 SpringMVCContract Encoder 解码器,用于将独享转换成HTTP请求消息体 SpringEncoder Decoder 编码器,将相应消息体转成对象 ResponseEntityDecoder Logger 日志管理器 Slf4jLogger RequestInterceptor

Spring RestTemplate Send List an get List

▼魔方 西西 提交于 2019-12-01 22:57:45
问题 I want to make a service with Spring's RestTemplate , in my service side the code is like this : @PostMapping(path="/savePersonList") @ResponseBody public List<Person> generatePersonList(@RequestBody List<Person> person){ return iPersonRestService.generatePersonList(person); } In client side if I call the service with this code: List<Person> p = (List<Person>) restTemplate.postForObject(url, PersonList, List.class); I can't use the p object as List<Person> , it will become a LinkedHashList .

Serializing a field as json

淺唱寂寞╮ 提交于 2019-12-01 22:43:08
I need to send a JSON body to REST service. Unfortunately, service is quite old and I need to send a POJO, containing JSON string field. So it looks like this: class SomeData { int id; SomePojo jsonField; ... } So SomeData should be sent like this: {'id': 1, 'jsonField': some_json_string} I haven't found any Jackson magic annotation to make it works and I've got a doubt it can be made somehow because of type erasure in Java and it may not be possible to write custom serializer for this purpose but I'm not sure. Could you please suggest any idea of how I can make it work or workaround the issue

Spring RestTemplate Send List an get List

我的梦境 提交于 2019-12-01 21:50:14
I want to make a service with Spring's RestTemplate , in my service side the code is like this : @PostMapping(path="/savePersonList") @ResponseBody public List<Person> generatePersonList(@RequestBody List<Person> person){ return iPersonRestService.generatePersonList(person); } In client side if I call the service with this code: List<Person> p = (List<Person>) restTemplate.postForObject(url, PersonList, List.class); I can't use the p object as List<Person> , it will become a LinkedHashList . After some research I find a solution that said I have to call the service with exchange method: