Spring MVC GetMapping does not take effect - spring

I'm trying Spring MVC,I tried #RequestMapping and #GetMampping annotation. There is something wired that #GetMapping doest not take effect.
Below is the code:
#Controller
#RequestMapping(path="/service")
public class ServiceEntry {
#RequestMapping(path="/hello")
#ResponseBody
public String mainPage(){
return "Hello,Information";
}
#RequestMapping(path="/hello2", method = RequestMethod.GET)
#ResponseBody
public String page2(){
return "hello2!";
}
#GetMapping(path="/test1")
#ResponseBody
public String page_test1(){
return "Get method 1";
}
#GetMapping(path="/test2")
#ResponseBody
public String page_test2(){
return "Get method 2";
}
}
While I visit /service/hello or /service/hello2 it works, but /service/test1 or /service/test2 does not work, please help me, thanks!

In RequestMapping you can use parameter path but not in
GetMapping. You can do like this: GetMapping("/test1") or
GetMapping(value="/test1")

Use value instead path. EG: #GetMapping(value="/test2")

Related

DTO has only null with GET request params, but not POST #RequestBody

I'm trying to get my query params in a DTO like in this question but my DTO has always null value.
Is there anything wrong in my code ? I made it as simple as possible.
Queries:
GET http://localhost:8080/api/test?a=azaz => null
POST http://localhost:8080/api/test with {"a":"azaz"} => "azaz"
Controller with a GET and a POST:
#RestController
#RequestMapping(path = {"/api"}, produces = APPLICATION_JSON_VALUE)
public class MyController {
// GET: dto NOT populated from query params "?a=azaz"
#RequestMapping(method = GET, path = "test")
public #ResponseBody String test(TestDto testDto){
return testDto.toString(); // null
}
// POST: dto WELL populated from body json {"a"="azaz"}
#RequestMapping(method = POST, path = "test")
public #ResponseBody String postTest(#RequestBody TestDto testDto){
return testDto.toString(); // "azaz"
}
}
DTO:
public class TestDto {
public String a;
#Override
public String toString() {
return a;
}
}
Thanks !
Full Spring boot sample to illustrate it
The problem is that you are missing setter for the field.
public void setA(String a) {
this.a = a;
}
should fix it.
I'm assuming that you have done required configuration like having Jackson mapper in the class path, consume json attribute, getter and setter in DTO classes etc.
One thing missed here is, in RequestMapping use value attribute instead of path attribute as shown below
#RequestMapping(method = POST, value= "/test", consumes="application/json")
public #ResponseBody String postTest(#RequestBody TestDto testDto){
return testDto.toString();
}
And, make sure that you set content-type="application/json" while sending the request
I think what you are trying to do is not possible. To access the query Parameter you have to use #RequestParam("a"). Then you just get the String. To get your object this way you have to pass json as Parameter. a={"a":"azaz"}
Kind regards

spring url mapping when #PathVariable is empty

For the following url mappings (all GET requests):
1. "/book/{book-id}"
2. "/book/publisher/{publisher-id}"
3. "/book/borrower/{borrower-id}"
If 2. or 3. are called with empty string e.g. /book/publisher/ or /book/user/ then the forward slash / is ignored and 1. is invoked with book-id as 'publisher' or 'borrower'. Is this a correct behaviour because a path variable is mandatory rendering 1 as the only valid url in this situation, or is there some config that controls it?
This is a simple rest controller that returns JSON (code simplified):
#RestController
public class BookController {
#Autowired
BookRepository repository; //JPA repository
#RequestMapping(value="/book/{book-id}",method= RequestMethod.GET)
public Book getBook(#PathVariable("book-id") String bookId){
return repository.findOne(bookId);
}
#RequestMapping(value="/book/publisher/{publisher-id}",method=RequestMethod.GET)
public List<Book> getBooksByPublisher(#PathVariable("publisher-id") String publisherId){
return repository.findAll(publisherId);
}
#RequestMapping(value="/book/borrower/{borrower-id}",method= RequestMethod.GET)
public List<Book> getBooksForBorrower(#PathVariable("borrower-id") String borrowerId){
return repository.findAll(borrowerId);
}
}
You can control the flow like this in your getBook() method
if(bookId.equals("publisher") || bookId.equals("borrower")){
model = new ModelAndView("index");
//or do other code to controll the flow
return model;
}

Spring MVC parse web url Object

I have a GET request in the format below
http://www.example.com/companies?filters=%7B%22q%22%3A%22aaa%22%7D
After decode it is
filters={"q":"aaa"}
I have created an Object named Filters as below
public class Filters {
private String q;
//getter setter....
}
and in my controller
#RequestMapping(method = RequestMethod.GET)
public List<CompanyDTO> getCompanies(Filters filters) {
filters.getQ();
//do things
}
However, the filters.getQ() is null.
Am I doing something incorrect here?
You need to associate the request parameter to the method argument. Add #RequestParam to your method i.e.
#RequestMapping(method = RequestMethod.GET)
public List<CompanyDTO> getCompanies(#RequestParam(value="filters") Filters filters) {
filters.getQ();
//do things
}
Instead of #RequestParam, use #RequestBody
Instead of String filters=%7B%22q%22%3A%22aaa%22%7D, pass JSON object as parameter http://www.example.com/companies?filters={"q":"aaa"}

Bind map and list in spring mvc

Controller:
#ResponseBody
#RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(StressTest stressTest) {
//dosomething
return "ok";
}
Bind Class:
Class StressTest {
private Map<String,List<String>> methods;
private String serviceName;
...
}
Question is:
How to write html form,my http post package :
methods['GetSome'].[0].value=ds&methods['GetSome'].[1].value=asaaaaa&serviceName=aa
But bind fail in springmvc,List in methods always empty
Posted data are not correct. Get rid of the .value part
Try to produce this:
methods['GetSome'].[0]=ds&methods['GetSome'].[1]=asaaaaa&serviceName=aa
Use methods['GetSome'][0]=ds. If use List ,use methods['GetSome'][0].someMember.

Request parameters in spring

I need to take two parameters in my spring controller.
http://mydomain.com/myapp/getDetails?Id=13&subId=431
I have controller which will return Json for this request.
#RequestMapping(value = "/getDetails", method = RequestMethod.GET,params = "id,subId", produces="application/json")
#ResponseBody
public MyBean getsubIds(#RequestParam String id, #RequestParam String subId) {
return MyBean
}
I am getting 400 for when i tried to invoke the URL. Any thoughts on this?
I was able to get it with one parameter.
Try specifying which parameter in the query string should match the parameter in the method like so:
public MyBean getsubIds(#RequestParam("id") String id, #RequestParam("subId") String subId) {
If the code is being compiled without the parameter names, Spring can have trouble figuring out which is which.
As for me it works (by calling: http://www.example.com/getDetails?id=10&subId=15):
#RequestMapping(value = "/getDetails", method = RequestMethod.GET, produces="application/json")
#ResponseBody
public MyBean getsubIds(#RequestParam("id") String id, #RequestParam("subId") String subId) {
return new MyBean();
}
P.S. Assuming you have class MyBean.

Resources