today at 11:04:06Dec 06, 2022 11:04:06 AM org.apache.catalina.core.StandardService stopInternal
today at 11:04:06INFO: Stopping service [Tomcat]
today at 11:04:062022-12-06 11:04:06.846, [main], ERROR, org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter, 40, [report]
today at 11:04:06
today at 11:04:06***************************
today at 11:04:06APPLICATION FAILED TO START
today at 11:04:06***************************
today at 11:04:06
today at 11:04:06Description:
today at 11:04:06
today at 11:04:06Field httpUtil in com.lanxi.hm.service.impl.MedicineBoxServiceImpl required a bean of type 'com.lanxi.hm.util.HttpUtil' that could not be found.
today at 11:04:06
today at 11:04:06The injection point has the following annotations:
today at 11:04:06 - @org.springframework.beans.factory.annotation.Autowired(required=true)
today at 11:04:06
today at 11:04:06
today at 11:04:06Action:
today at 11:04:06
today at 11:04:06Consider defining a bean of type 'com.lanxi.hm.util.HttpUtil' in your configuration.
之前的代码:
@Autowired
private HttpUtil httpUtil;
修改之后的代码:
private HttpUtil httpUtil;
HttpUtil代码
import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
public class HttpUtil {
private final RestTemplate restTemplate;
private HttpHeaders defaultHeader;
private final String baseUrl;
private HttpUtil(RestTemplate restTemplate, String baseUrl, HttpHeaders defaultHeader) {
this.restTemplate = restTemplate;
this.defaultHeader = defaultHeader;
this.baseUrl = baseUrl;
}
private HttpHeaders resolveHeaders(HttpHeaders headers) {
if (headers != null) {
if (this.defaultHeader != null) {
headers.addAll(this.defaultHeader);
}
return headers;
}
return this.defaultHeader;
}
private URI resolveUri(String url) {
if (StringUtils.isNotBlank(baseUrl)) {
return URI.create(baseUrl + url);
}
return URI.create(url);
}
public ResponseEntity<String> get(String url) {
return this.execute(url, HttpMethod.GET, null, null, String.class);
}
public ResponseEntity<String> get(String url, HttpHeaders headers) {
return this.execute(url, HttpMethod.GET, null, headers, String.class);
}
public <R> ResponseEntity<R> get(String url, HttpHeaders headers, Class<R> responseBodyType) {
return this.execute(url, HttpMethod.GET, null, headers, responseBodyType);
}
public <E> ResponseEntity<String> post(String url, E body) {
return this.execute(url, HttpMethod.POST, body, null, String.class);
}
public <E> ResponseEntity<String> post(String url, E body, HttpHeaders headers) {
return this.execute(url, HttpMethod.POST, body, headers, String.class);
}
public <E, R> ResponseEntity<R> post(String url, E body, HttpHeaders headers, Class<R> responseBodyType) {
return this.execute(url, HttpMethod.POST, body, headers, responseBodyType);
}
public ResponseEntity<String> execute(String url, HttpMethod method, String body, HttpHeaders headers) {
return this.execute(url, method, body, headers, String.class);
}
public <T> ResponseEntity<T> execute(String url, HttpMethod method, Object body, HttpHeaders headers, Class<T> responseBodyType) {
URI uri = resolveUri(url);
RequestEntity<Object> requestEntity = new RequestEntity<>(body, resolveHeaders(headers), method, uri);
return restTemplate.exchange(requestEntity, responseBodyType);
}
public void addDefaultHeaders(HttpHeaders headers) {
if (this.defaultHeader == null) {
this.defaultHeader = headers;
}
if (headers != null) {
this.defaultHeader.addAll(headers);
}
}
public void addDefaultHeader(String header, String value) {
if (this.defaultHeader == null) {
this.defaultHeader = new HttpHeaders();
}
this.defaultHeader.add(header, value);
}
public void removeDefaultHeader(String header) {
if (this.defaultHeader != null) {
this.defaultHeader.remove(header);
}
}
public static HttpUtil newInstance(@NonNull RestTemplate restTemplate) {
return new HttpUtil(restTemplate, null, null);
}
public static HttpUtil newInstance(@NonNull RestTemplate restTemplate, @Nullable String baseUrl) {
return new HttpUtil(restTemplate, baseUrl, null);
}
public static HttpUtil newInstance(@NonNull RestTemplate restTemplate, @Nullable String baseUrl, @Nullable HttpHeaders defaultHeader) {
return new HttpUtil(restTemplate, baseUrl, defaultHeader);
}
}