符合REST客户要求的带有resttemplate Whitelabel错误页面的SpringREST API

时间:2018-09-17 12:18:52

标签: java spring-mvc swagger thymeleaf resttemplate

您好,您目前正在使用Swagger的api,但出现了白色错误页面 这是我的客户API服务           包装服务;

    import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import domains.Customers;


@Service
public class CustomersApiService {

//  define a RestTemplate object
private final RestTemplate restTemplate;


//  web service resources endpoints
private final String GET_ALL_USER = 
"https://api.predic8.de/shop/customers/";


 //  define an argument constructor, then pass in the RestTemplate object 
   and  
Autowire it
@Autowired
public CustomersApiService (RestTemplate restTemplate){
    this.restTemplate = restTemplate;
}

//  Get All Users
//  @returns a list
@SuppressWarnings("unchecked")
public List<Customers> findAllCustomers(){



   return Arrays.stream(restTemplate.getForObject
(GET_ALL_USER,Customers[].class)).collect(Collectors.toList());
}

}

这是我的客户总监     程序包控制器;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import services.CustomersApiService;

@Controller
public class CustomersController {

private final CustomersApiService service;

@Autowired
public CustomersController(CustomersApiService service) {
    this.service = service;
}

@GetMapping("/customers")
public String getAll(Model model){
    model.addAttribute("customers", service.findAllCustomers());
    return "customers";
}

} 这是我的客户POJO       包域;

import java.util.HashMap;
import java.util.Map;

public class Customers {

private String firstname;
private String lastname;
private String customerUrl;
private Map<String, Object> additionalProperties = new HashMap<String, 
Object>();

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getCustomerUrl() {
return customerUrl;
}

public void setCustomerUrl(String customerUrl) {
this.customerUrl = customerUrl;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

这是客户文件

<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Users</title>
 <!-- Bootstrap core CSS -->
<link href="..static/css/bootstrap.min.css" 
th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
<link href="..static/css/bootstrap-theme.min" 
th:href="@{/css/bootstrap.min.css}" rel="stylesheet">


<link href="..static/css/carousel.css" th:href="@{/css/carousel.css}" 
rel="stylesheet">
</head>
<body>
    <div class="container">
          <div class="jumbotron">
            <h1>Java SringREST API </h1> 

          </div>
    </div>

<div class="container">
    <div th:if = "${not #lists, isEmpty (customers)}">
        <h1>Users List</h1>
            <table class="table table-hover">
            <tr>
              <th>FirstName</th>
              <th>LastName</th>
              <th>CustomerUrl</th>
            </tr>
            <tr th:each="Customer : $(customers)">
              <td th:text = "${customers.firstname}"></td>
              <td th:text = "${customers.lastname}"></td>
              <td th:text = "${customers.customerUrl}"></td>
            </tr>

          </table>
     </div>
     </div>
  <!-- FOOTER -->
  <footer class="container">
    <p class="float-right"><a href="#">Back to top</a></p>
    <p>&copy; 2017-2018 Company, Inc. &middot; <a href="#">Privacy</a> 
  &middot; <a href="#">Terms</a></p>
  </footer>


<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="..static/js/jquery.min.js" th:src="@{/js/jquery.min.js}"> 
</script>
<script src="..static/js/bootstrap.min.js" th:src="@{/js/bootstrap.min.js}"> 

运行此春季应用程序后,出现白色标签错误 这个网址http://localhost:8080/customers,         这是浏览器上的错误     白标签错误页面

This application has no explicit mapping for /error, so you are seeing 
this as a fallback.

Wed Sep 19 10:39:31 PDT 2018
There was an unexpected error (type=Not Found, status=404).
No message available

pls我不知道为什么我在页面上出现此白色错误,希望它使用API​​返回客户详细信息。有帮助吗?

1 个答案:

答案 0 :(得分:0)

首先,您需要创建一个客户POJO,在我的情况下,我已将Recipe作为POJO来使用restTemplate

$.getJSON('data.json', function(data) {
    destinations = data[]

    $.each(destinations, function(id, destination) {
        destination = destination[]
    })
});

然后创建一个实现类:

@Configuration
public class RestTemplateConfig {



    //Creating a restTemplate Bean 

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder)
    {
        return builder.build();
    }


}

这只是一个演示类,您可以使用restTemplate,我希望它对您有用。