SpringBoot - CMS structure [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am implementing a Spring Boot application that will be a micro-service.
This micro-service is supposed to define a CRUD #RestController over a CMS.
Is there any standard way of doing this, or any library I can take advantage of?
i.e:
#RequestMapping(value = "/simple/{key}", method = RequestMethod.GET)
public String getKey(#PathVariable final String key) {
return getCmsKey(key);//Retrieve key
}
#RequestMapping(value = "/collection/{key}", method = RequestMethod.GET)
public List<String> getKeys(#PathVariable final String key) {
return getCmsListOfKeys(keys);//Retrieve collection of keys
}

Related

How to make one method return different values for user with role ADMIN and USER? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I need to make the #Get method, and for users with ROLE_USER it should return other values than for users with ROLE_ADMIN.
The URL have to be the same.
How to do it with Spring Security?
If your authentification passed well you could just inject #AuthenticationPrincipal:
#GetMapping("/get-url-here")
public String main(#AuthenticationPrincipal User user) {
if (user.getRole().equals("ROLE_ADMIN")) {
// set values for admin
} else {
// set for user
}
return "view-or-response-body";
}
I assume that you have the User class configured, like:
public class User {
public String role;
// other fields, getters, setters
}
Or role could be enum as well.
Question is not 100% clear, but what I would do is make an Enum field in the user class (assuming that class exists) for the role and return the value of that field when the URL is called.

How to get Spring bean by name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can I get the bean by passing only the class name using BeanFactoryUtils
I'm trying below, but this is not working
import org.springframework.beans.factory.BeanFactoryUtils;
baseDao= BeanFactoryUtils.originalBeanName("RegionDaoImpl");
RegionDao
#Component
public class RegionDaoImpl implements BaseDao<Region> {
...
}
Any suggestions?
You need a ListableBeanFactory, then you call beanOfType(), e.g.:
RegionDaoImpl dao = BeanFactoryUtils.beanOfType(beanFactory, RegionDaoImpl.class);
Generally, the ListableBeanFactory will be an ApplicationContext, so you need the application context of your Spring application. How to get that depends on your application type, and where the code calling beanOfType() is located.
It is usually better to let Spring auto-wire the object into your class, which does the same thing, i.e. lookup the bean by type.
#Component
public class SomeComponent {
#Autowire
private RegionDaoImpl regionDao;
...
}
If you want to lookup by name, you'd call beanFactory.getBean(), but that seems kind of redundant:
RegionDaoImpl dao = beanFactory.getBean("RegionDaoImpl", RegionDaoImpl.class);

how to use a post method with a spring data rest process? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm using a repostiroy rest resource service and currently I need to implement a method to import the data from a excel file into my database. I don't see how I can do with spring repository rest resource? since it's an interface. do I use restController or does it have a way to do both?
The standard flow would be:
Create a repository file
#Repository
public interface RepositoryClass extends JpaRepository<T, ID>{}
where T is the class and ID is the data type of the ID (ex: Long, Integer etc.)
The JpaRepository already have save() and saveAll() methods(1)
In service create a method that read the data and saves it in the database
#Service
public class ServiceClass{
// ...some code
public void importData(// ... parameters){
// open the excel file and import the data in an ArrayList for exemple
repositoryClass.saveAll(arrayWithData);
}
}
The last step is to create a endpoint in the controller which calls the method that is written in service.
That interface have a method called save(). You can save the given object in database like: repository.save(myOjbect)

What best way to pass #PathVariable to my all JSP pages without add it to ModelAndView? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I change my path from:
/user/home
/user/history
...
to
/{orgId}/home
/{orgId}/history
...
So for all /{orgId}/* pages I need orgId on my JSP page to construct right links. How to do it without to get #PathVariable in each method and pass it to ModelAndView.
Thanks!
Use ControllerAdvice and ModelAttribute
#ControllerAdvice
class Advice {
#ModelAttribute
public void addAttributes(Model model) {
model.addAttribute("orgId", "value1");
}
}
As of Spring 4, #ControllerAdvice can be customized through annotations(), basePackageClasses(), basePackages() methods to select a subset of controllers.

Looking for method argument validation framework based on AOP and annotations [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I'm looking for argument validation framework which:
1) Allows specifying argument constraints via annontations (like OVal, JaValid)
2) Validation code automatically injected (during compilation or runtime) into methods (i.e. no explicit call to Validator object is required)
Example of what i'm looking for:
public class Person {
private String name;
....
//Method which arguments should be validated
public void setName(#NotBlank String name){
//<---validating code should be injected here
this.name = name;
}
}
//Example of call to the validated method
...
Person person = new Person();
person.setName("John");
...
Example of code i'm trying to avoid
...
Validator validator = new Validator(...);//glue code
Person person = new Person();
person.setName("John");
validator.validate(person);//glue code
...
Thanks for answers!
I think you meant "automatically injected during compilation or runtime", right?
I had the same problem. My solution was Spring Validation and self-written AOP layer (about three classes).
My validation code looks like this:
#Validational( validators =
{"com.mycompany.MyValidator"} )
public void myMethod( String paramToValidate )

Categories