spring-rest

Return list of errors

丶灬走出姿态 提交于 2019-12-11 07:33:16
问题 I wan to return a list of errors using this Response object: public class StringResponseDTO { private String response; public StringResponseDTO(String response) { super(); this.response = response; } public String getResponse() { return response; } public void setResponse(String response) { this.response = response; } } I use this code to generate errors: List<FieldError> errors = result.getFieldErrors(); for (FieldError error : errors ) { System.out.println ("Validation error in field: " +

Corda RPC JacksonSupport.createDefaultMapper to use ObjectMapper in Spring client

南楼画角 提交于 2019-12-11 06:55:07
问题 How to register newly introduced Corda RPC ObjectMapper in Spring Boot? Even after having below code in @Configuration class Jackson failing to serialize Party object to JSON string. @Bean public JsonComponentModule jsonComponentModule() { return new JsonComponentModule(); } @Bean @Primary public ObjectMapper cordaRpcObjectMapper(NodeRPCConnection rpc) { ObjectMapper objectMapper = JacksonSupport.createDefaultMapper(rpc.getProxy(), new JsonFactory(), true); objectMapper.registerModule

Unmarshalling response depending on HTTP code during Spring Rest Service call

五迷三道 提交于 2019-12-11 05:57:17
问题 Calling a Rest Webservice using the Spring Rest Template as follows- ResponseEntity<String> response = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, entity, String.class); and get the output in String format as <Info xmlns="http://schemas.test.org/2009/09/Tests.new" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <FirstName>FirstName</FirstName> <LastName>LastName</LastName> <TestGuid>Guid</TestGuid> <TestUID>1</TestUID> <Token>token</Token> <TestUserID>14<

DELETE in Spring RestTemplate with HttpEntity<List>

房东的猫 提交于 2019-12-11 04:32:14
问题 I don't know why my code is not working, I've tried with Postman and works fine: But with RestTemplate I can´t get a response while it´s using the same endpoint... . ResponseEntity<String> responseMS = template.exchange(notificationRestService, HttpMethod.DELETE, new HttpEntity<NotificationRestDTO[]>(arrNotif), String.class); I've tried with List instead Array[] When i made a PUT request it´s works fine but with one object: ResponseEntity<String> responseMS = template.exchange

How to configure jackson property naming strategy?

瘦欲@ 提交于 2019-12-11 01:30:22
问题 This code does not work: @Configuration public class RepositoryRestMvcConfig extends RepositoryRestMvcConfiguration { ... @Bean @Override public ObjectMapper objectMapper() { ObjectMapper mapper = super.objectMapper(); mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); return mapper; } ... } How to configure Jackson property naming strategy with JavaConfig? 回答1: You can use below approach for naming strategy : class MyBean { private String

Disable Keycloak authentication for a specific url in spring-boot

做~自己de王妃 提交于 2019-12-10 22:28:44
问题 Front end of my spring-boot service gets rendered in a 3rd party dashboard. That dashboard also has a generic search bar which we want to use. Now, once we implemented Keycloak authentication, we started facing problems specifically in this search bar. All the other API's works fine because they are called from my front end only, But search API gets called by the 3rd party dashboard. Weirdly, 3rd party calls my method using Http OPTION method, but my endpoint is registered as GET. For an

Error on generating self link on pageable resource

非 Y 不嫁゛ 提交于 2019-12-10 16:39:21
问题 Make a simple RestController @RestController public class Controloler @Value class MyData { int value; } @GetMapping(value = "/datas", produces = MediaTypes.HAL_JSON_VALUE) public PagedResources<Resource<MyData>> getMyData(PagedResourcesAssembler<MyData> assembler, @RequestParam(required = false) String param, @PageableDefault Pageable pageRequest) { MyData data = new MyData(1); Page<MyData> page = new PageImpl<>(Collections.singletonList(data), pageRequest, 100); Link selfLink = linkTo

Spring REST Upload file as binary

主宰稳场 提交于 2019-12-10 15:28:29
问题 I'm using spring. I want to implement rest controller to upload file to server. I found a lot examples like this: public ResponseEntity doSomething(@PathVariable String paramOne, @RequestParam(required = false, name="file") List<MultipartFile> attachments ) throws IOException { //Some logic here } Then I test it with postman, I create a request of type "form-data", add pram name "file", select type file, and select file. And it works ok. It creates a post request as multipart request. But for

Conditionally returning both JSON and (HTML) templates from @RestController in Spring Boot

百般思念 提交于 2019-12-10 11:26:31
问题 Most of the similar questions seem to have the opposite problem I’m having. I’m building a Spring Boot-based webapp using @RestController . The JSON responses work well, but now I want to support returning HTML via templates (Thymeleaf, specifically). All the examples show building methods like this: @RequestMapping(method = RequestMethod.GET) String index() { return "index"; } And that works fine, so long as the class it’s in is annotated with @Controller . If I annotate with @RestController

Spring MVC ExceptionHandler for restful and normal

爱⌒轻易说出口 提交于 2019-12-10 09:50:26
问题 I want to handle exception for both normal and rest/ajax requests. Here is my code, @ControllerAdvice public class MyExceptionHandler { @ExceptionHandler(Exception.class) public ModelAndView handleCustomException(Exception ex) { ModelAndView model = new ModelAndView("error"); model.addObject("errMsg", ex.getMessage()); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); ex.printStackTrace(pw); sw.toString(); model.addObject("errTrace", sw); return model; }