I would like to write whole controller to work with entity.
I would like to declare the id of entity on class level and use it on each method. Here is the controller class:
#Controller
#RequestMapping(value="/job/{j_id}/instance")
public class JobController extends GenericController {
private final String htmlDir = "job/";
#RequestMapping(value="{i_id}/open", method=RequestMethod.GET)
public ModelAndView open(#PathVariable Long instance_id) {
ModelAndView result = new ModelAndView(htmlDir + "instance");
result.addObject("instance_id", instance_id);
Here I would like to use variable j_id from #RequestMapping
return result;
}
}
Can I achive this? Please help. Give me some code snippest please.
Have a try like this
#Controller
#RequestMapping(value="/job/{j_id}/instance")
public class JobController {
private final String htmlDir = "job/";
#RequestMapping(value="{i_id}/open", method=RequestMethod.GET)
public ModelAndView open(#PathVariable(value="j_id") Long instance_id) {
ModelAndView result = new ModelAndView(htmlDir + "instance");
result.addObject("instance_id", instance_id);
System.out.println("Instance Id -------------> " + instance_id);
return result;
}
}
Please notic "#PathVariable(value="j_id")"
To get both variables, you can change that line as following:
#RequestMapping(value="{i_id}/open", method=RequestMethod.GET)
public ModelAndView open(#PathVariable(value="j_id") Long jnstance_id, #PathVariable(value="i_id") Long instance_id) {
.....
}
Related
Inside the MVC controller , I have a service that I access to get data:
#Controller
#AllArgsConstructor
public class DataController {
private SharedDataService sharedDataHolder;
#RequestMapping(value = "/testPoint", method = GET)
public String getData(#PathVariable String point, Model model){
Data data = sharedDataHolder.getData();
...
}
}
and SharedDataService :
#Service
public class SharedDataService {
private Data data;
public Data getData(){
if(isHourAgo()){
this.refreshData();
}
return data;
...
}
}
And actually the question is, should I make the data variable of the SharedDataService class volatile so that there is no conflict inside the controller?
Data :
public class Data {
private String title;
private Map<String,Double> characteristics;
}
I want to test my controller using postman but don't know how to send a model attribute using postman.
I tried to send all attributes in row json fornamt and x-www-form-urlencoded in body but it is not working for me, I didn't understand where i'm getting wrong
My controller class looks like :
#RestController
public class DemoController {
#Autowired
private DemoService demoService;
#RequestMapping(value = "/userDetail", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON })
public String testme(
ModelMap model,
#ModelAttribute("inputParameter") InputParameter inputParameter,
BindingResult result) {
return demoService.getDetail(inputParameter);
}
}
Model Class :
public class InputParameter {
private String id;
private String name;
private String number;
private String address;
private String pass;
}
I am using Spring Framework with restful web services, and I am trying to create an API with restful service and use a get method. I have created a method and I'm trying to have it return a string, but instead I get a 404 error - requested resources not found. Please see my code below:
#RestController
#RequestMapping("/test")
public class AreaController {
public RestResponse find(#PathVariable String name, ModelMap model) {
model.addAttribute("movie", name);
return "list";
}
}
I am using: localhosr:8080/MyProject/wangdu
This error occurs because you forgot to add
#RequestMapping(value = "/{name}", method = RequestMethod.GET) before your find method:
#RestController
#RequestMapping("/test")
public class AreaController {
#RequestMapping(value = "/{name}", method = RequestMethod.GET)
public RestResponse find(#PathVariable String name, ModelMap model) {
model.addAttribute("movie", name);
return "list";
}
}
Please make sure about this:
The value that the find method is returning is a String with the value "list" and the find method declaration is waiting for a RestResponse object
For example if I have a RestResponse object like this:
public class RestResponse {
private String value;
public RestResponse(String value){
this.value=value;
}
public String getValue(){
return this.value;
}
}
Then try to return the value in this way:
public RestResponse find(#PathVariable String name, ModelMap model) {
model.addAttribute("movie", name);
return new RestResponse("list");
}
Verify that the method has #RequestMapping annotation with the value that your expect from the url
#RequestMapping(method = RequestMethod.GET, value = "/{name}")
By default the proper way to call the rest resource is by the #RequestMapping value that you set at the #RestController level (#RequestMapping("/test")), in this case could be: http://localhost:8080/test/myValue
If you need to use a different context path then you can change it on the application.properties (for spring boot)
server.contextPath=/MyProject/wangdu
In that case you can call the api like this:
http://localhost:8080/MyProject/wangdu/test/myValue
Here is the complete code for this alternative:
#RestController
#RequestMapping("/test")
public class AreaController {
#RequestMapping(method = RequestMethod.GET, value = "/{name}")
public RestResponse find(#PathVariable String name, ModelMap model) {
model.addAttribute("movie", name);
return new RestResponse("list");
}
Anyone know if it`s possible to create one method in my entity, to execute when I put the annotation #Valid in my class?
Example:
I have this Object:
public class Area {
#NotEmpty
private String unidade;
#NotNull
private double tamanho;
public String getUnidade() {
return unidade;
}
public void setUnidade(String unidade) {
this.unidade = unidade;
}
public double getTamanho() {
return tamanho;
}
public void setTamanho(double tamanho) {
this.tamanho = tamanho;
}
}
And I have this method:
#RestController
#RequestMapping("/recolhimento")
public class RecolhimentoController {
#RequestMapping(method = RequestMethod.GET)
public boolean getRecolhimento(#Valid Area area){
...
}
}
so when I call this method the Spring Boot will validate my model Area( but I want to create one method that will be execute when I use #Valid.
it`s possible? how?
Yes, it is possible.
You can find examples in this project: https://github.com/malkusch/validation
i try make a generic controller with some methods, so i don´t need re-writer common codes, but don´t work, why???
#Controller("/home/teste")
public class CtrlTeste extends ControladorGenericoSpring<Assistenciado>
{
public String path;
public CtrlTeste()
{
super(Assistenciado.class);
path = "/home/teste";
setPacoteServico("servico.assistenciado");
setPrefixo("Serv");
setNomeEntidade("Assistênciado");
}
#RequestMapping
public String teste(#RequestParam(value = "id", required= true)Long id, Model model)
{
Assistenciado ass = getServico().buscarPorId(id);
model.addAttribute("assistenciado", ass);
return "/home";
}
}
You're currently specifying the name of the bean. Try with:
#Controller
#RequestMapping("/home/teste")
public class CtrlTeste extends ControladorGenericoSpring<Assistenciado> {
...
}