Java Spring REST, value from object (POST) - spring

I have a quick question. I`m sending two params in a json object (user) from angular to my spring app using POST. When I display this object it is:
System.out.println(user.email);
{email=example#example.com, password=gfdgdfgf}
In Java my code is:
#RequestMapping(value="/userLogin", method = RequestMethod.POST)
public boolean userLogin(#RequestBody Object user, ModelMap model) {
...
}
But when I try to display the field from this object like:
user.password, it doesn`t work.
Many thanks for help!

You are mapping your data to an Object, which lacks the needed fields(email and password). You should create an entity class and use it instead. Jackson should be able to map your passed parameters.
public class User{
public String email;
public String password;
/* ... */
}

Related

Validatng enums in Spring validation

I'm doing request parameter validation from a Spring controller. I have an Enum validator, similar to https://funofprograming.wordpress.com/2016/09/29/java-enum-validator/, which works fine if the enum field is directly in the object I'm using for validation. But it doesn't work if that object contains other objects.
For example, here is the request in the Controller
#PostMapping("/")
public ResponseEntity<?> performOperation(#Valid #RequestBody MyModel model) {
Here is the model I'm using to validate the request params
#ApiModel
public class MyModel {
#ApiModelProperty
#EnumValueValidator(enumClass = EnumName.class)
public String provider;
MyObject obj;
}
public class MyObject {
#EnumValueValidator(enumClass = SomeEnum.class)
public String anotherEnum;
}
In the above example, provider is validated with no problem. But anotherEnum is not. Is there a way for a Spring model to do a deep validation into objects?
You should annotate MyObject obj with #Valid annotation as well. Just keep in mind that null objects are not validated, so probably you should do both:
#NotNull
#Valid
MyObject obj;

How to send model attribute to spring controller

I want to test my controller using postman but don't know how to send a model attribute using postman. I even don't know whether it is possible or not.
My Controller seems like:
#Controller
#RequestMapping(path = "/api/v1")
public class PaymentController {
#Autowired
private CredentialsRepository credentialsRepository;
#PostMapping(path = "/charge")
public String charge(#ModelAttribute("pay-load") PayLoad payLoad, Model model) {
Credentials creds = credentialsRepository.findCredentialsById(1);
if (creds == null)
return "init_credentials";
return "charge";
}
}
Model Attribute
public class PayLoad {
private Integer mId;
private Integer ordId;
private Integer cardId;
private Integer cvvNo;
private String hash;
// getter & setter
}
I found the way to send model attributes to the spring controller.
see above screenshot for your reference.
Even you can pass all the key and value in requestParam from postman.
Instead of requestBody. ModelAttribute object treat each and every key and value as requestParam. It's just a way to combine a lot of requestParam in one object. Even you can try out with curl request, so it will make you more clear.
Thanks

Using Custom Object wrapper for Request Body

I have a Spring service with the following API:
/v1/createUser
Request Body
{
"UserId" : "some-guid-value",
"Username" : "username",
"password" : "hashed-password"
}
The UserId in the body is optional. The other values are mandatory. I would like my API controller to be defined like this:
#RequestMapping(method = RequestMethod.POST, value = "v1/createUser")
#ResponseBody
public void createUser(
#RequestBody CreatUserRequest body)
Now, my question is, how do I (or is it even possible to) create the CreateUserRequest class such that Spring will reject the request if it doesn't see Username and password as part of the body. UserId is optional and may or may not be present.
Thanks!
Try
public class CreatUserRequest {
#NotBlank
private String username, password;
private UUID userId;
// getter and setter
}
I would complete Peter's Answer.
1. Decalre your condition using :#NotBlank in your DTO.
2. Validate entry on you rest controller through #Validlike this:
public void createUser(#RequestBody #Valid CreatUserRequest body)
This should work.

Sprinng 4.X passing variables with #Pathvariable annotation

I want to pass some variables to my server. I did it this way, like shown in the example:
#Controller
#RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {
#RequestMapping("/pets/{petId}")
public void findPet(#PathVariable String ownerId, #PathVariable String petId, Model model) {
// implementation omitted
}
}
This works totally fine when I send a request like this:
domain/owners/123/pets/123
But what I want to do is getting all pets of one owner. This means I dont need/want to pass a pet-ID:
domain/owners/123/pets/
But then I get an Excpetion that there is no Handler for this request. Is it possible to send a request like this or is it limited by Spring?
You have to add a second method:
#RequestMapping("/pets/")
public void findPetByOwner(#PathVariable String ownerId Model model) {
// implementation omitted
}

From request object to the database

I have an app with an AngularJS front-end and a Spring MVC back-end. I'm having some trouble with converting/mapping request objects to domain/dto objects.
On one page you can add a new order to the system, the POST payload would look something like this:
{
memo: "This is some extra info for order",
orderLines: [{productId:3, quantity:4}, {productId:2, quantity:5}, {productId:1, quantity:4}],
shippingDate: "2014-10-08T19:16:19.947Z",
warehouseId: 2
}
The Spring MVC controller method looks like this:
#RequestMapping(value = "/order", method = RequestMethod.POST)
public ResponseEntity<Void> addOrder(#RequestBody #Valid OrderRequest orderRequest, UriComponentsBuilder b) throws Exception {
// the magic
}
Where OrderRequest is filled with the values of the POST request, the OrderRequest and OrderLineRequest look like this:
public class OrderRequest {
private Long id;
private Date shippingDate;
private String memo;
private List<OrderLineRequest> orderLines;
private Long warehouseId;
public OrderRequest() {
}
// getters and setters ommitted
}
public class OrderLineRequest {
private Long id;
private String productCode;
private int quantity;
public OrderLineRequest() {
}
}
My question now is, in order to save an Order object with orderService.add(order) I need to construct the Order object based on the values that were sent in the request. Where/how do I do this?
OPTION 1
The OrderRequest class could have a makeOrder() method with just returns an Order object like so:
public Order makeOrder() {
Order order = new Order();
order.setMemo(this.memo);
order.setShippingDate(this.shippingDate);
...
}
Then I'd have to map the OrderLineRequest which could have their own makeOrderLine method:
public OrderLine makeOrderLine() {
OrderLine orderLine = new OrderLine();
orderLine.setQuantity = this.quantity;
...what to do with only the productId?
}
As you can see I can set the quantity but in the request I only received the productId, but in the database I save the productCode, productName as well, so I need that info from the database, but I don't want to make a database call from the Request object...I also don't want to half of the mapping in the request object and the rest of the mapping in the controller where I do have access to the services.
OPTION 2
I can use Dozer to do the mapping for me, but that would mean injecting the services into the Dozer custom converters which seem equally unclean to me...
OPTION 3
I pass the OrderRequest object to the service layer and let the service layer handle it, but my question would remain, how exactly would the service layer convert it, say you have the method addOrder like this:
public void addOrder(OrderRequest orderRequest) {
}
Would you call another service to convert from one to the other as I don't really want this conversion in a business logic method?
Any help would be appreciated
use the #RequestBody to map your jsonObject that is send with the request , to a DTO .
please refer to the following tutorial .
hope that helps .
and please ask if there is something not clear .

Resources