简体   繁体   English

尝试与resttemplate一起使用put时无法读取HTTP消息

[英]Getting failed to read HTTP message, while trying to use put with resttemplate

I am learning spring boot. 我正在学习弹簧靴。 And while updating a rest service below with resttemplate 并且在使用resttemplate更新以下休息服务时

RestTemplate restTemplate = new RestTemplate();

//        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
//        restTemplate.getMessageConverters().add(new StringHttpMessageConverter());


        HttpHeaders httpHeaders = restTemplate.headForHeaders("http://localhost:8080/getallblade");

        List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        messageConverters.add(new FormHttpMessageConverter());
        messageConverters.add(new StringHttpMessageConverter());

        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.ALL));

        messageConverters.add(new MappingJackson2XmlHttpMessageConverter());

        messageConverters.add(converter);  
        restTemplate.setMessageConverters(messageConverters);

        BladeView blade  = new BladeView();
        blade.setBladeId(id);
        blade.setAppStatus(false);
        blade.setOperStatus(false);
        blade.setPowerStatus(powerstatus);


        HttpEntity<BladeView> requestUpdate = new HttpEntity<>(blade, httpHeaders);

        restTemplate.exchange("http://localhost:8080"+"/addd", HttpMethod.PUT, requestUpdate, Void.class);

I have tried many version of the above code with put instead of exchange but getting the same error as below: 我尝试使用put而不是exchange来尝试上述代码的许多版本,但得到的错误如下:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: java.io.PushbackInputStream@696b5835; line: 1, column: 1]
org.springframework.web.client.HttpClientErrorException: 400 null
...................
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:539)
    at com.rhevm.server.COTSSERVER.updatePowerStatus(COTSSERVER.java:102)

These are my controller,services and model 这些是我的控制器,服务和模型

@RequestMapping(value="/addd",method=RequestMethod.PUT,consumes = "application/json", produces = "application/json")
    public void add(@RequestBody List<BladeView> bladeView) {
         System.out.println(bladeView);
         bladeViewService.add(bladeView);
    }




public void add(List<BladeView> bladeView) {
         baldeViewRepository.save(bladeView);
     }


@Entity
@Table(name="bladeview")
public class BladeView {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer bladeId;

Could you please guide as I am not able to solve this from days. 请问您能不能提供指导,因为几天后我将无法解决此问题。

you can go throw like this 你可以这样扔

  HttpHeaders httpHeaders = restTemplate.headForHeaders("http://localhost:8080/getallblade");
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<String> entity = new HttpEntity<String>(jsonRequest, headers);

        BladeView blade  = new BladeView();
        blade.setBladeId(id);
        blade.setAppStatus(false);
        blade.setOperStatus(false);
        blade.setPowerStatus(powerstatus);
        ArrayList<BladeView> list = new ArrayList();
        list.add(blade)

        ObjectMapper om = new ObjectMapper();
        String jsonRequest = jsonObjectMapper.writeValueAsString(blade);
        restTemplate.put(uri, entity);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SpringBoot:无法读取HTTP消息 - SpringBoot :Failed to read HTTP message 使用RestTemplate时出现消息“不支持HTTP协议”的异常 - Getting Exception with message “http protocol is not supported” when using RestTemplate 尝试通过Springs RestTemplate执行POST请求时获取RestClientException - Getting a RestClientException while trying to perform a POST request with Springs RestTemplate 尝试通过Selenium和Java使用sendKeys时获取验证消息 - Getting validation message while trying to use sendKeys through Selenium and Java 无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException 尝试从SSLSocket读取时获取EOFException - Getting EOFException while trying to read from SSLSocket 尝试从MQ读取消息时收到始终为空的消息 - Receives always null message while trying to read a message from MQ 春季-无法读取HTTP消息。 缺少所需的请求正文 - Spring - Failed to read HTTP message. Required request body is missing 无法读取HTTP消息。 缺少所需的请求正文 - Failed to read HTTP message. Required request body is missing 调用restTemplate.exchange()时获取IllegalStateException - Getting IllegalStateException while calling restTemplate.exchange()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM