我们正在使用Spring Boot进行应用程序.在ApplicationConfig.java中,我有以下代码
@Bean
public LocaleResolver localeResolver() {
return new SmartLocaleResolver();
}
Run Code Online (Sandbox Code Playgroud)
并且SmartLocaleResolver.java位于下方
public class SmartLocaleResolver extends SessionLocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
final String acceptLanguage = request.getHeader("Accept-Language");
if (acceptLanguage.contains(",")) {
String[] aheader = acceptLanguage.split(",[ ]*");
for (String locale : aheader) {
if (ApplicationConstants.LOCALE.contains(locale)) {
locale.trim();
return Locale.forLanguageTag(locale);
}
}
} else if (!acceptLanguage.contains(",") && !acceptLanguage.isEmpty()) {
if (ApplicationConstants.LOCALE.contains(acceptLanguage)) {
return Locale.forLanguageTag(acceptLanguage);
}
}
return request.getLocale();
}
}
Run Code Online (Sandbox Code Playgroud)
我在下面的常量类中比较了标题Accept-Language中的值.
public static final List LOCALE = Collections .unmodifiableList(Arrays.asList("en","es"));
我知道在实际情况下,标题将像Accept-Language:fr,es; q …
我正在使用弹簧靴.我有一个休息的api POST调用,我需要使用x-www-form-urlencoded发送正文,调用不需要头文件.我可以从邮递员中获取此URL,并在db中成功创建一行.但是当我尝试从Java端调用它时,我收到400个错误的请求异常.
以下是我在Java中尝试过的内容.
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("param1", "123");
map.add("param2", "456");
map.add("param3", "789");
map.add("param4", "123");
map.add("param5", "456");
HttpHeaders headers = new HttpHeaders();
final HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String, String>>(map,
headers);
try {
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.postForObject(
"https://url", entity,
String.class);
System.out.println(response);
} catch (final Exception e) {
throw new RuntimeException();
}
Run Code Online (Sandbox Code Playgroud)
我得到了这个例外.
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
Run Code Online (Sandbox Code Playgroud)
我也尝试过如下,并得到了同样的例外.
ResponseEntity<String> x = restTemplate.exchange("https://url", HttpMethod.POST,
map, String.class);
Run Code Online (Sandbox Code Playgroud)
这是完整的堆栈跟踪
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) …Run Code Online (Sandbox Code Playgroud) 我正在使用Mockito进行单元测试,我得到以下异常.
org.mockito.exceptions.base.MockitoException:
`'setResponseTimeStampUtc'` is a *void method* and it *cannot* be stubbed with a *return value*!
Voids are usually stubbed with Throwables:
doThrow(exception).when(mock).someVoidMethod();
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. The method you are trying to stub is *overloaded*. Make sure you are calling the right overloaded version.
2. Somewhere in your test you are stubbing *final methods*. Sorry, Mockito does not verify/stub …Run Code Online (Sandbox Code Playgroud) 我们在项目中使用Spring云.我们有几个微服务,每个服务都有自己的.yml文件.
以下属性仅在zuul服务器中
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
ConnectTimeout: 3000
ReadTimeout: 60000
Run Code Online (Sandbox Code Playgroud)
测试1:
账户服务:
我正在调用此服务来测试超时,我通过zuul调用请求,即使用端口8006.
@RequestMapping(value = "/accountholders/{cardHolderId}/accounts", produces = "application/json; charset=utf-8", method = RequestMethod.GET)
@ResponseBody
public AllAccountsVO getAccounts(@PathVariable("cardHolderId") final String cardHolderId,
@RequestHeader("userContextId") final String userContextId,
@RequestParam final MultiValueMap<String, String> allRequestParams, final HttpServletRequest request) {
return iAccountService.getCardHolderAccountsInfo(cardHolderId, userContextId, request, allRequestParams,
ApplicationConstants.ACCOUNTHOLDER);
}
Run Code Online (Sandbox Code Playgroud)
上面的服务使用Spring RestTemplate在内部调用下面的服务.我开始测试时通过在关联服务中添加如下所示的5000毫秒的休眠时间并向Accounts Service(getAccounts调用)发出请求.
协会服务:
@RequestMapping(value = "/internal/userassociationstatus", produces = "application/json; charset=utf-8", consumes = "application/json", method = RequestMethod.GET)
@ResponseBody
public UserAssociationStatusVO getUserAssociationStatus(@RequestParam final Map<String, String> allRequestParams) {
try …Run Code Online (Sandbox Code Playgroud) spring-boot hystrix microservices spring-cloud netflix-ribbon
我正在为项目使用Spring Boot,并尝试加载yaml文件,以便可以在项目中使用文件中的数据。例如,我的application.yml中包含如下内容。
currency:
code:
840: 2
484: 2
999: 0
Run Code Online (Sandbox Code Playgroud)
在我从application.yml读取内容的代码中,我有一个类似的类。
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "currency")
public class Currency {
private Map<String, String> code;
public Map<String, String> getCode() {
return code;
}
public void setCode(Map<String, String> code) {
this.code = code;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在测试课中打印
public class Test{
@Autowired
Currency currency;
Map<String, String> test = currency.getCode();
for (Map.Entry<String, String> entry : test.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
} …Run Code Online (Sandbox Code Playgroud) java ×4
spring-boot ×4
rest ×3
spring ×3
web-services ×2
hystrix ×1
junit ×1
localization ×1
mockito ×1
spring-cloud ×1