spring-restcontroller

Java RestController REST parameter dependency injection or request

為{幸葍}努か 提交于 2021-02-11 17:19:25
问题 I am trying inject a dependency or at least filtrate the ID parameter that come into a RestControler in Spring. I am very new in Spring. How can i be sure that the parameter that comes passed in the API is valid and/or how can i inject its dependency related to Customer Entity? This is my rest controller CustomerController method @PatchMapping("/{id}") public Customer updateCustomer(@PathVariable Long id, @RequestBody Customer customer) { return customerService.updateCustomer(id, customer); }

Starting Spring boot REST controller in two ports

若如初见. 提交于 2021-02-11 15:35:21
问题 Is there a way to have two rest controller running on two different ports from one spring boot application ? Say for example Controller_A running in http://localhost:8080 and Controller_B running in http://localhost:9090 in one SpringBoot main Application ? 回答1: One way of doing this is actually creating two application properties; app-A.properties server.port=8080 app-B.properties server.port=9090 And then in your controllers, put annotation like below; @Profile("A") public class ControllerA

What is the best practice for RestController?

南笙酒味 提交于 2021-02-07 12:39:23
问题 Code convention says no logic in the controllers. All should be handled in the service layer. My question is especially about returning ResponseEntity. Should it be handled in RestController or in Service layer? I tried both ways. I think RestController is the suitable place to return ResponseEntity. Because we are using mappings in the RestController. On the other hand, we know the controllers should not include any logic. @GetMapping("/{id}") public ResponseEntity<Employee> getEmployee(

Jackson also needs getter methods to correctly serialize a bean property using @JsonCreator

99封情书 提交于 2021-02-05 07:36:18
问题 I am using Jackson to serialize some beans into JSON, inside an application that is using Spring Boot 1.5. I noticed that to serialize a bean using the @JsonCreator correctly, I have to declare the getter method for each property, plus the @JsonProperty annotation. public class Person { private final String name; private final int age; @JsonCreator public Person(@JsonProperty("name") String name, @JsonProperty("age") int age) { this.name = name; this.age = age; } public String getName() {

PUT makes POST instead of updating value in Spring Boot

走远了吗. 提交于 2021-01-29 21:56:08
问题 I don't understand why, but whenever I do put operation in postman this method always creates new vehicle This is code for put mapping @PutMapping("/{id}") public Vehicle updateVehicle(@PathVariable long id, @RequestBody Vehicle vehicle){ vehicle.setVehicleId(id); return vehicleRepository.saveAndFlush(vehicle); } This is code for post mapping @PostMapping("") public Vehicle newVehicle(@RequestBody Vehicle vehicle){ return vehicleRepository.saveAndFlush(vehicle); } 来源: https://stackoverflow

Exception in Spring AOP Itself not getting handled through RestControllerAdvice

早过忘川 提交于 2021-01-29 13:42:32
问题 I need to validate the HttpServletRequest for certain attribtues in my service layer before proceeding. I created @Before advice, Please note that AOP method throws Exception. I want exception thrown by this method should be handled by RestControllerAdvice. I can see that AOP method is being executed but DataNotValidException is not being handled by RestControllerAdvice. RestConrollerAdvice is working fine if there is any exception on the validation of other parameters or any exception in

How do I read the post data in a Spring Boot Controller?

99封情书 提交于 2021-01-28 01:41:51
问题 I would like to read POST data from a Spring Boot controller. I have tried all the solutions given here: HttpServletRequest get JSON POST data, but I still am unable to read post data in a Spring Boot servlet. My code is here: package com.testmockmvc.testrequest.controller; import org.apache.commons.io.IOUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import

How to implement and limit API calls per second in Spring Rest

眉间皱痕 提交于 2021-01-27 12:36:32
问题 I have spring batch application with spring MVC. In this application, I have to call Google API. There is a restriction of max 4 req per sec for API. Now I have to call google API from inside the spring batch. So I have two questions. q1: How can I implement rest call to Google API. I know about Rest Template but I want that there is any better approach like feign client that we use in microservices. q2: how can I restrict 4 calls per second. In case you have any question. Please let me know

what is difference between X-Auth-Token vs Authorisation headers.Which is preferred

时光怂恿深爱的人放手 提交于 2021-01-20 14:32:33
问题 What is the difference between the two headers below? Which one is preferred? X-Auth-Token : dadas123sad12 Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== 回答1: Authorization is the primary header used by clients to authenticate against peers in HTTP as foreseen in RFC 7235. It is often linked to the Basic authentication scheme as per RFC 7617, but that is not a given. The Basic scheme allows clients to provide a username-password-pair separated by a colon ( : ) coded in Base64. It cannot be

Configure FAIL_ON_UNKNOWN_PROPERTIES for each RequestMapping differently in the Controller

陌路散爱 提交于 2020-12-06 12:16:15
问题 I want to handle json to Object conversion differently on different @RequestMapping in my Controller. I believe if we add Jackson dependency in our spring-boot project it handles json to Object conversion and #spring.jackson.deserialization.fail-on-unknown-properties=true property will make sure that conversion will fail if there is some unknown property present in the json (please correct me if I am wrong). Can we tell jackson locally when to fail on unknown properties and when to ignore