What does {id} mean/do in #RequestMapping(value = "/delete/{id}") - spring

I'm currently in the process of learning Spring and I keep bumping into that in various examples but I haven't found any explanation anywhere
I've also found
return "redirect:/delete/{id}"
I understand id is a variable, but what does it do. what is the difference between
#RequestMapping(value = "/delete/{id}")
and
#RequestMapping(value = "/delete")
Taken from this example http://www.javainterviewpoint.com/spring-mvc-crud-example-mysql/
Controller
#RequestMapping(value = "/delete/{id}")
public ModelAndView deleteEmployee(#ModelAttribute("employee") Employee employee,#PathVariable("id") int id)
{
employeeDAO.deleteEmployee(id);
return new ModelAndView("redirect:/employees");
}
Model
package com.javainterviewpoint;
import java.io.Serializable;
public class Employee implements Serializable
{
private static final long serialVersionUID = -1280037900360314186L;
private Integer id;
private String name;
private Integer age;
private String dept;
public Employee()
{
super();
}
public Employee(Integer id, String name, Integer age, String dept)
{
super();
this.id = id;
this.name = name;
this.age = age;
this.dept = dept;
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}

#RequestMapping(value = "/delete/{id}") mean that you can delete employee with specifc id. The full request can be for example: http://yourSite.com/delete/42
Then you can see #PathVariable("id") int id - it's mean variable id will be contain value from url.
With this aproach you can do something like this:
/doSomething/{someUserName}/{someValue}/{someId}
and you will have:
#PathVariable("someUserName") String someUserName, #PathVariable("someValue") String id, #PathVariable("id") int id
Another example:

It's a PathVariable and you can use it for example when you have users and you to need to edit or delete one user. This will tell to spring method on which user you have clicked.
You can read the section URI Template Pattern of Spring MVC guide for more clarifications.
With this approach you attach the id of the user to the url you are calling, and Spring will map the id found in url to the variable you define in method deleteEmployee (#PathVariable("id") int id)
For example you can invoke deleteEmployee method with this url:
http://yourUrl/delete/12345
Then employeeDAO.deleteEmployee(id); call will be executed with 12345 as id

Related

Spring boot using Autowire Cofiguration property class in Entity class

I have to use some set data member class in Spring entity class
Current Entity class
Entity(name="users")
public class Users{
#Id
#GeneratedValue
#Column(name=ID")
private long Id;
#Column(name=NAME")
private String name;
#Column(name=AGE")
private String age;
#Column(name=PIN")
private String pin;
public Users(String name, String age, String pin)
{
this.name = name;
this.age = age;
this.pin = pin;
}
}
Now I need to add a new Member which is unique to that place
areaId, we run sperate application per each area so this will be passed from command line arguments or config properties during application starts.
My properties class looks like below
#Component
#ConfigurationProperties("user.info")
public class UserProperties{
public String areaId;
public String getAreaID(){return this.areaId;}
public void setAreaID(String areaId){ this.areaId = areaId;}
}
users:
info:
areaId:124
I have to store this and initializes also during Users object constructing, here I am trying to make simple
Entity(name="users")
public class Users{
#Id
#GeneratedValue
#Column(name=ID")
private long Id;
#Column(name=NAME")
private String name;
#Column(name=AGE")
private String age;
#Column(name=PIN")
private String pin;
#Column(name=AREAID")
private String areaId;
public Users(String name, String age, String pin)
{
this.name = name;
this.age = age;
this.pin = pin;
this.areaId = ""//?? how to get area id directly ?
}
}
I can not change the constructor of Users because it demands changes in the other application which are using this lib
Want to Autowire a users properties class inside Entity class(but this is not suggestable as read in some articles )
What would be the best way to initialize that default kind of variable?
It seems that your property is something static.
Then get it from a static way at start.
There's several ways to get command line value at start directly or in a static block:
static {
(your code here)
}
You'll can put #Column on a getter on this property after that.

MyBatis #Many / Spring-Boot

I'm beginner (sorry for my bad explanation, feel free to correct me) in MyBatis Spring-Boot, I have problem to understand and make it works #Many
I'm using 3 layer logic programming (Presentation Layer, Service Layer, Data Layer)
Thanks for your help :)
I have 3 Tables (it's TB_Products and not TB_Product as on the screenshot):
I would like to get data form table TB_Users and TB_Products to "put" it in DTO
I create 4 java object class SearchEntity, ProductEntity (for Data layer)
I create an interface SearchRepositoryMapper.
I also create a SearchService interface and SearchServiceImpl as well.
Java object class:
SearchEntity
public class SearchEntity implements Serializable{
private static final long serialVersionUID = -9143930742617602050L;
private String id;
private String firstName;
private String lastName;
private List<ProductEntity> products;
// Getters and Setters code .....
}
ProductEntity
public class ProductEntity implements Serializable{
private static final long serialVersionUID = -6525703679290992635L;
private String id;
private String productId;
private String product;
private String number;
private String date;
private String description;
// Getters and Setters code .....
}
SearchRepositoryMapper
public interface SearchRepositoryMapper {
// Get some fields from TB_Users and all fields from TB_Products
#Select("SELECT * FROM TB_Users WHERE id = #{id}")
#Results({
#Result(property = "id", column ="id"),
#Result(property = "firstName", column = "firstName"),
#Result(property = "lastName", column= "lastName"),
#Result(property = "products", javaType = List.class, column="id",
many = #Many(select = "getProductIdByUserId"))})
public SearchEntity findAllInfoByUserId(#Param("id") int id);
#Select("SELECT *, productId FROM TB_Products WHERE productId = #{id}")
public ArrayList<ProductEntity> getProductIdByUserId(#Param("id") int id);
// Find id by uderId and return null if it doesn't exist
#Select("SELECT id FROM TB_Users WHERE userId = #{userId}")
int findIdByUserId(#Param("userId") String userId);
}
SearchServiceImpl
#Service
public class SearchServiceImpl implements SearchService {
#Autowired
SearchRepositoryMapper searchRepository;
#Override
public SearchDto getAllInfoByUserId(String id) {
SearchDto returnValue = new SearchDto(); // Init returnValue as SearchDto
int searchId = searchRepository.findIdByUserId(id); // Init searchId with the TB_Users id
SearchEntity searchEntity = searchRepository.findAllInfoByUserId(searchId);
BeanUtils.copyProperties(searchEntity, returnValue);
return returnValue;
}
}
So when I execute the code and do a GET request I get this error message:
{
"message": "nested exception is org.apache.ibatis.executor.ExecutorException: Statement returned more than one row, where no more than one was expected."
}
I found out that come from the mapper and SearchEntity searchEntity = searchRepository.findAllInfoByUserId(searchId);
But i don't know how to resolve it. The way I wrote the code is wrong
Thanks to correct me
The exception clearly says that the query returns multiple results. Plese verify if the data in the table is correct.

SpringBoot concatenate search parameters browser url

I am starting working with Spring Boot. My aim is to make a limited search retrieving data from a database. I want to add multiple parameters in the query of the url.
So far I was able using the seek: http://localhost:8080/wsr/search/, to get a full search of the data in the database. But what I want is delimit the search under several conditions adding parameters in the url in the browser as for instance:
http://localhost:8080/data/search/person?name=Will&address=Highstreet&country=UK
http://localhost:8080/data/search/person?name=Will&name=Angie
http://localhost:8080/data/search/person?name=Will&name=Angie&country=UK
The problem I found is that I can't find the way to work with more than one condition. The only thing I got to make it work, is:
http://localhost:8080/data/search/person?name=Will
I surfed the web but no results for this exact problem, too much information but impossible to find this.
The code I have is:
#Entity
#Table(name = "person")
public class Person {
#Id
#GeneratedValue
#Column(name = "id")
private int id;
#Column(name = "name")
private String name;
#Column(name = "address")
private String address;
#Column(name = "country")
private String country;
public Value() {
}
public Value(int id, String name, String address, String country) {
this.id = id;
this.name = name;
this.address = address;
this.country = country;
}
//all getters and setters
}
public class Implementation {
#Autowired
private DataBase dataBase;
public List<Value> findById(#PathVariable final int id) {
return dataBase.findById(id);
}
public List<Value> findByName(#PathVariable final String name) {
return dataBase.findByName(name);
}
public List<Value> findByAddress(#PathVariable final String address) {
return dataBase.findByAddress(address);
}
public List<Value> findByCountry(#PathVariable final String country) {
return dataBase.findByCountry(country);
}
}
//#Component
#RepositoryRestResource(collectionResourceRel = "person", path = "data")
public interface DataBase extends JpaRepository<Value, Integer>{
public List<Value> findAll();
#RestResource(path = "ids", rel = "findById")
public List<Value> findById(#Param("id") int id) throws ServiceException;
#RestResource(path = "name", rel = "findByName")
public List<Value> findByName(#Param("name") String name) throws ServiceException;
#RestResource(path = "address", rel = "findByAddress")
public List<Value> findByAddress(#Param("address") String address) throws ServiceException;
#RestResource(path = "country", rel = "findByCountry")
public List<Value> findByCountry(#Param("country") String country) throws ServiceException;
}
Hope you can help me putting me in the correct way of what should do or is wrong. If possible some code will also be highly appreciated.
Best regards
You can use #RequestParam("nameParameter")annotation to map all the parameters you want. Let's say you have url like :
http://localhost:8080/data/search/person?name=Will&country=UK
then you can have an api like:
...
#RequestMapping(value = "/person")
public String api(#RequestParam("name") String name, #RequestParam("country") String country)
...

Get URL parameter for crit use Spring MVC Hibernate

I want to be list out all my users with criteria of where id = formId. The code is working but just that it list out all the users instead of being filtered by formId. Please tell me where i did wrongly. Do tell me if you need any more info to solve this!
controller
*url = http://localhost:8080/User/Panda?Id=1
#RequestMapping(value = {"/{name}?Id={id}" }, method = RequestMethod.GET)
public String listClinicUser(ModelMap model, #PathVariable("id") Integer id) {
logger.info("Users List Page - Id = " + id);
List<User> user = service.findAllUsers(id);
model.addAttribute("users", user);
return "user/list";
}
Service
public List<User> findAllUsers(Integer id) {
return dao.findAllUsers(id);
}
DAO Class
public interface UserDao {
List<User> findAllUsers(Integer id);
}
*DAOImpl Class
#SuppressWarnings("unchecked")
public List<User> findAllUsers(Integer id) {
Criteria crit = createEntityCriteria();
crit.add(Restrictions.eq("formId",id));
crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<User> users = (List<Usert>) crit.list();
return users;
}
*for createEntityCriteria() i created in another class call abstractDao and extends to it.
private final Class<T> persistentClass;
#SuppressWarnings("unchecked")
public AbstractDao(){
this.persistentClass =(Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
}
protected Criteria createEntityCriteria(){
return getSession().createCriteria(persistentClass);
}
Class Entity
#Entity
#Table(name="USER")
public class User implements Serializable{
#NotEmpty
#Column(name="formId", nullable=false)
private Integer formId;
#NotEmpty
#Column(name="FIRST_NAME", nullable=false)
private String firstName;
#NotEmpty
#Column(name="LAST_NAME", nullable=false)
private String lastName;
public Integer getFormId() {
return formId;
}
public void setFormId(Integer formId) {
this.formId= formId;
}
...
}
value = {"/{name}?Id={id}" }
This is wrong way to extract URL param. If you want to get URL param, you should pass it to your method using #RequestParam annotation:
#RequestMapping(value = {"/{name}" }, method = RequestMethod.GET)
public String listClinicUser(ModelMap model, #RequestParam("Id") Integer id) {
//...
}
Spring automatically pass value that you need. For example in case of ?Id=1 Spring will pass 1 to your controller
In your url /{name} is a path variable and is annotated with #PathVariable like in:
#RequestMapping(value = "/foo/bar/{name}", method = GET)
#ResponseBody
public String getBarByName(#PathVariable String name) { ... }
And ?Id=id is a request parameter and is annotated wiht #RequestParam so if we map to url like this one:
http://localhost:8080/api/foo/bar?id=100
we do it like this
#RequestMapping(value = "/foo/bar", method = GET)
#ResponseBody
public String getBarById(#RequestParam("id") Integer id) { ... }
So to combine them to map to your url:
#RequestMapping(value = {"/{name}" }, params = "id", method = RequestMethod.GET)
public String listClinicUser(ModelMap model, #PathVariable String name, #RequestParam("id" Integer id)) { ... }

How I can get one column value from a table?

I use Spring boot and Spring Data.
I have a contact entity with the id and firstName columns.
#Entity
#Table(name = "Contact")
public class Contact {
#Id
#GeneratedValue(strategy= GenerationType.AUTO)
private int id;
private String firstName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
I use repository with Spring Data to find my data.
public interface contactRepository extends CrudRepository<Contact, Long> {
}
My controller, example with getAll:
#RequestMapping(value = "/getAllContact", produces = "application/json")
public List<Contact> getAllClients(){
return repo.getAll();
}
My controller works but I don't know how to return all values in column firstName in my controller. I tried with a query, It works but it only returns a list of values and not the json:
#Query(value = "SELECT firstName FROM Contact" )
List<Contact> findAllFirstName();
Example:
["Pierre", "Jean"]
And i want this (in Json):
[{"firstName ": "Pierre" },{"firstName ":"Jean"}]
How do I do this?
Use the projection and excerpt support in Spring Data Rest. Whilst adding in JsonIgnore annotations does work, it's inflexible as you can only ignore at compile time not run time.
See
http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts
Make sure jackson libraries are in your classpath. Then add #ResponseBody in your controller method to return json output. Also add #JsonIgnore in id in your entity to exclude it from json output.
#RequestMapping(value = "/getAllContact", produces = "application/json")
#ResponseBody
public List<Contact> getAllClients(){
return repo.getAll();
}
#Entity
#Table(name = "Contact")
public class Contact {
#Id
#GeneratedValue(strategy= GenerationType.AUTO)
#JsonIgnore
private int id;
private String firstName;
.....
}

Resources