如何i18n spring @RestController javax.validation约束? 我以为我可以添加/src/main/resources/messages.properties和messages_de.properties,然后spring可以检测到它们并启用适当的I18N?但这似乎并非如此: @RestController public void TestController { @PostMapping public void post(@Valid @RequestBody Person p) { } } public class Person { private String firstname; @javax.validation.constraints.NotBlank(message = "{errors.person.lastname}") private
以下是关于 spring-web 的编程技术问答
我有这个春季启动应用程序,其中我在方法内有以下行. BufferedReader reader = new BufferedReader(new FileReader("somePath")); 如何将其注入我的代码中,以便可以为单元测试嘲笑它吗?使用Guice我可以使用提供商.但是如何使用Spring Boot实现这一目标?任何帮助将不胜感激. 解决方案 如果要模拟方法内有以下行的类. BufferedReader reader = new BufferedReader(new FileReader("somePath")); 创建类的模拟实例,并定义模拟行为,例如: @MockBean private TestClass testClass; when(testClass.readFile()).thenReturn(content); 内容是您要返回的输出. 您可以创建一个缓冲读取器的豆子,并注入: @Bean Buffe
我有一个简单的Spring Boot Web应用程序.我正在尝试从服务器接收一些数据.控制器返回一个集合,但是浏览器接收空的JSON-卷曲括号的数量等于服务器的对象数,但其内容为空. @RestController public class EmployeeController { @Autowired private EmployeeManagerImpl employeeManagerImpl; @RequestMapping(path="/employees", method = RequestMethod.GET) public Iterable getAllEmployees() { Iterable employeesIterable = employeeManagerImpl.getAllEmployees(); return employeesIterable; }
我正在尝试上传具有文本字段和使用Multipart/form-data作为内容类型的映像的表单.当我从Angular应用程序中执行帖子时,我可以在控制台中清楚地看到内容类型是正确的,并且请求有效载荷由2个名为Seedimage和SeedDetails的不同部分组成.这是2个屏幕截图,说明了这一点: 请求标头: 请求有效载荷: 但是,当请求到达我的春季启动应用程序时,它会引发此异常. 2018-07-07 12:18:24.061 DEBUG 25956 --- [nio-8080-exec-2] .w.s.m.m.a.ServletInvocableHandlerMethod : Failed to resolve argument 0 of type 'org.springframework.web.multipart.MultipartFile' org.springframework.web.multipart.support.MissingServlet
我正在尝试通过Exchange方法使用Spring RESTTEMPLATE进行http删除请求,但是我总是得到一个400不良请求,例如这个问题.使用JavaScript和其他工具,可以使用DELETE进行此API调用. 我知道java 我的代码: public DealResponse closePosition(DealCloseRequest dealCloseRequest) { try { ObjectMapper mapper = new ObjectMapper(); //Object to JSON in String String jsonInString = mapper.writeValueAsString(dealCloseRequest); HttpEntity entity = new HttpEntity(jsonInString, this.he
我正在使用Spring Resttemplate来提出HTTPS请求,我想忽略SSL证书 这是我创建RESTEMPLATE请求的代码: TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true; SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom() .loadTrustMaterial(null, acceptingTrustStrategy) .build(); SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext); loseableHttpClient httpClient = HttpClients.custom() .setSSLSocketFactory(csf) .bu
我有一个@RestController Web服务方法,该方法可能会用长期运行的服务调用阻止响应线程.如下: @RestController public class MyRestController { //could be another webservice api call, a long running database query, whatever @Autowired private SomeSlowService service; @GetMapping() public Response get() { return service.slow(); } @PostMapping() public Response get() { return service.slow(); } } 问题:如果X用户在这里调用我的服务怎么办?执行线程将全部阻止,直到返回
我正在尝试使用@PropertySource和Environment变量加载config.proprties文件数据. 示例: serverName = abc\xyz 当我使用方法阅读此属性时 String server= env.getProprty("serverName"); System.out.print(server); 值打印为" abcxyz". 请注意,我尝试使用Double Backsslash,例如 serverName = abc\\xyz ,但仍然只是从值字符串中忽略\. 另外,我不能用前向斜线代替后斜线. 您能帮我解决吗?预先感谢! 解决方案 这是一个真正的丑陋hack,但是您可以尝试使用unicode逃生序列作为" \ u005c"符号" \ \ u005c",因此而不是字符串值" abc \ xyz"使用" abc \ u005cxyz" .但是再说一次,它将将其
我有一些字段的简单json消息,并希望使用spring-web>. 将其映射到Java对象 问题:我的目标类字段的命名与INT JSON响应不同.无论如何,我如何将它们映射到对象,而无需重命名Java中的字段? 是否可以在这里放置一些注释? { "message":"ok" } public class JsonEntity { //how to map the "message" json to this property? private String value; } RestTemplate rest = new RestTemplate(); rest.getForObject(url, JsonEntity.class); 解决方案 将JSON属性映射到具有不同名称的Java对象 @jsonproperty注释 ,您的代码将: public class JsonEntity { @JsonProperty(value
我正在创建一个带有Spring Boot和Web依赖项的简单REST控制器.我正在尝试将JSON主体验证为只有3个字段的测试POJO,但是当我尝试提出邮政请求时,服务器以500错误响应,并且我在控制台中遇到的错误是: .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.LinkedHashMap 我编写的所有代码如下: emailapplication.java: package com.test.email.app; import org.springframework.boot.CommandLineRunner;
testcontroller.java @RestController public class TestController { @Autowired private TestClass testClass; @RequestMapping(value = "/test", method = RequestMethod.GET) public void testThread(HttpServletResponse response) throws Exception { testClass.doSomething(); } } testclass.java @Component @Scope("prototype") public class TestClass { public TestClass() { System.out.println("new test class cons
我正在使用spring-boot,并在maven pom中添加了spring-web依赖性,以使用RestTemplate. 现在,春季试图初始化EmbeddedServletContext.我该如何防止它? Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. at org.springframework.boot.context.emb
我是春季的新手,并尝试使用RestTemplate进行REST请求. Java代码应与下面的curl命令相同: curl --data "name=feature&color=#5843AD" --header "PRIVATE-TOKEN: xyz" "https://someserver.com/api/v3/projects/1/labels" 但是服务器用400 Bad Request 拒绝RESTTEMPLATE RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.add("PRIVATE-TOKEN", "xyz"); HttpEntity entity = new HttpEntity("name=feature&color=#5843AD", headers); ResponseEnti
我有这个春季休息控制器方法: @ApiOperation("My method") @RequestMapping(method = RequestMethod.POST, value = "/myMethod") public void myMethod(@RequestParam("myParam") String myParam) { ... } 使用语言 java 和library RESTTEMPLATE : ,使用Swagger Codegen CLI生成剩余的客户端: public void myMethod(String myParam) throws RestClientException { ... return apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType,
我正在创建一个期望类型List的弹簧靴微服务REST API.如何验证列表包含最小值和最大值的量? 到目前为止,我尝试使用@Size(min=1, max=2)也应该支持收藏(javax.validation.constraints.Size). 我还尝试添加@Valid和a BindingResult参数以及@Size notation nosection nosect. 我更喜欢使用类似于第一个示例的解决方案,使用@Size(min=1, max=2)更紧凑且整洁.这是针对弹簧靴2.1.2.Release. @RestController public class MyApi { @GetMapping(value = "/myApi", produces = { APPLICATION_JSON_VALUE }) public ResponseEntity> getSomething(@Vali
在我的控制器中,我有一种创建实体的方法 import javax.validation.Valid; ... @RestController public class Controller { @RequestMapping(method = RequestMethod.POST) public ResponseEntity create(@Valid @RequestBody RequestDTO requestDTO) { ... import org.hibernate.validator.constraints.NotEmpty; ... public class RequestDTO @NotEmpty // (1) private String field1; //other fields, getters and setters. 我想添加一个控制器方法 update(@Valid @RequestBody