Как устранить ошибку: HttpClientErrorException: 400 ноль? - PullRequest
0 голосов
/ 18 апреля 2020

Я разработал службу покоя, которая управляет операциями с клиентами, которые касаются клиентов.

Теперь я хотел бы позвонить в службу покоя из моего весеннего приложения mvc, но я получаю следующую ошибку, когда нажимаю на кнопка «обновить», которая находится на моей jsp странице:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is
  org.springframework.web.client.HttpClientErrorException: 400 null
  org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
  org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
  org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
  org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) cause mère
    org.springframework.web.client.HttpClientErrorException: 400 null
    org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
    org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79)
    org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
    org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:766)
    org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:724)
    org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680)
    org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:332)
    com.luv2code.springdemo.service.CustomerServiceRestClientImpl.getCustomer(CustomerServiceRestClientImpl.java:59)
    com.luv2code.springdemo.controller.CustomerController.showFormForUpdate(CustomerController.java:62)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

, это мой класс DemoAppConfig:

package com.luv2code.springdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("com.luv2code.springdemo")
@PropertySource({ "classpath:application.properties" })
public class DemoAppConfig implements WebMvcConfigurer {

    // define a bean for ViewResolver

    @Bean
    public ViewResolver viewResolver() {

        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }

    // define bean for RestTemplate ... this is used to make client REST calls

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    // add resource handler for loading css, images, etc

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
          .addResourceHandler("/resources/**")
          .addResourceLocations("/resources/"); 
    }   
}

, а это мой файл application.properties:

#
# The URL for the CRM REST API
# - update to match your local environment
#
crm.rest.url=http://localhost:8080/spring-crm-rest/api/customers

и это мой класс обслуживания:

package com.luv2code.springdemo.service;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.luv2code.springdemo.model.Customer;

@Service
public class CustomerServiceRestClientImpl implements CustomerService {

    private RestTemplate restTemplate;

    private String crmRestUrl;

    private Logger logger = Logger.getLogger(getClass().getName());

    @Autowired
    public CustomerServiceRestClientImpl(RestTemplate theRestTemplate, 
                                        @Value("${crm.rest.url}") String theUrl) {
        restTemplate = theRestTemplate;
        crmRestUrl = theUrl;

        logger.info("Loaded property:  crm.rest.url=" + crmRestUrl);
    }

    @Override
    public List<Customer> getCustomers() {

        logger.info("in getCustomers(): Calling REST API " + crmRestUrl);

        // make REST call
        ResponseEntity<List<Customer>> responseEntity = 
                                            restTemplate.exchange(crmRestUrl, HttpMethod.GET, null, 
                                                                  new ParameterizedTypeReference<List<Customer>>() {});

        // get the list of customers from response
        List<Customer> customers = responseEntity.getBody();

        logger.info("in getCustomers(): customers" + customers);

        return customers;
    }

    @Override
    public Customer getCustomer(int theId) {

        logger.info("in getCustomer(): Calling REST API " + crmRestUrl);

        // make REST call
        Customer theCustomer = 
                restTemplate.getForObject(crmRestUrl + "/" + theId, 
                                          Customer.class);

        logger.info("in saveCustomer(): theCustomer=" + theCustomer);

        return theCustomer;
    }

    @Override
    public void saveCustomer(Customer theCustomer) {

        logger.info("in saveCustomer(): Calling REST API " + crmRestUrl);

        int employeeId = theCustomer.getId();

        // make REST call
        if (employeeId == 0) {
            // add employee
            restTemplate.postForEntity(crmRestUrl, theCustomer, String.class);          

        } else {
            // update employee
            restTemplate.put(crmRestUrl, theCustomer);
        }

        logger.info("in saveCustomer(): success");  
    }

    @Override
    public void deleteCustomer(int theId) {

        logger.info("in deleteCustomer(): Calling REST API " + crmRestUrl);

        // make REST call
        restTemplate.delete(crmRestUrl + "/" + theId);

        logger.info("in deleteCustomer(): deleted customer theId=" + theId);
    }

}

, и это мой контроллер, который находится в моей службе отдыха:

package com.luv2code.springdemo.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.luv2code.springdemo.entity.Customer;
import com.luv2code.springdemo.service.CustomerService;

@RestController
@RequestMapping("/api")
public class CustomerRestController {


    // importer la depende du service

    @Autowired
    private CustomerService customerService;

    // ajout de la methode qui permet de récupérer les customers (clients)

    @GetMapping("/customers")
    public List<Customer> getCustomers(){


        return customerService.getCustomers();
    }


    // récupérer un seul customer avec un id donné en parametre

    @GetMapping("/customer/{customerId}")
    public Customer getCustomer(@PathVariable int customerId) {

        Customer theCustomer = customerService.getCustomer(customerId);

        if(theCustomer == null) {
            throw new CustomerNotFoundException("Le client avec l'id :"+ customerId + " n'est pas trouvé");
        }


        return theCustomer;
    }

    // ajouter la méthode d'ajout d'un nouveau customer

    @PostMapping("/customers")
    public Customer addCustomer(@RequestBody  Customer theCustomer) {

        theCustomer.setId(0);

        customerService.saveCustomer(theCustomer);

        return theCustomer;
    }


    // modification d'un customer existent :

    @PutMapping("/customers")
    public Customer updateCustomer(@RequestBody  Customer theCustomer) {



        customerService.saveCustomer(theCustomer);

        return theCustomer;
    }

    // delete un customer en fonction de l'id donné :

    @DeleteMapping("/customers/{customerId}")
    public String DeleteCustomer(@PathVariable int customerId) {

        Customer theCustomer = customerService.getCustomer(customerId);

        if(theCustomer == null) {
            throw new CustomerNotFoundException("le customer avec l'id donnée n'a pas été trouvé");
        }

        customerService.deleteCustomer(customerId);;

        return "le client avec l'id" + customerId + " a été supprimé";
    }

}

вот ссылка на привод для тех, кто хочет проверить мою заявку:

https://drive.google.com/open?id=1Hko4iSjpR1qV7s1kfWVdLbotWzn1Cvx1

Может кто-нибудь помочь мне, пожалуйста?

1 Ответ

2 голосов
/ 19 апреля 2020

В трассировке стека ошибок вы можете видеть, что последняя строка вашего кода называется

com.luv2code.springdemo.service.CustomerServiceRestClientImpl.getCustomer(CustomerServiceRestClientImpl.java:59)

При поиске в этом методе я нахожу это объявленное тело функции.

// make REST call
Customer theCustomer = restTemplate.getForObject(crmRestUrl + "/" + theId, 
                                      Customer.class);

400 - это код состояния, поэтому restTemplate делает свой вызов правильно, но 4xx означает, что отвечающий сервер говорит, что запрос искажен или неверен со стороны клиента (поэтому это не ошибка отвечающего сервера). Итак, я начинаю смотреть на запрос. Вы используете два параметра: theId, который является входным параметром, а затем URL crmRestUrl.

При поиске, где эти параметры объявлены, я нахожу следующее.

private String crmRestUrl;

я считаю, что это null, и это может быть вашей проблемой.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...