Haiming Zhou
Haiming Zhou

Reputation: 15

Confusion for @RequestMapping solving one value and a list values

I have created a class called User, and I want to test @RequestMapping with POST and GET. I created 2 methods, one is for one object, and the other one is for a list of Objects. However, when I test GET on the return one object method which only allowed POST, it turns value back, and values are a list of objects. So why does it cause?

@RestController
public class WebController {
 @PostMapping(name="/getUser123")
    public User getUser(){
        User user=new User();
        user.setName("mint");
        user.setAge(19);
        user.setPass("123456");
        return user;
    }

    @RequestMapping(name="/changeWholeName")
    public List<User> changeWholeName(){
        List<User> users =new ArrayList<User>();
        User user1=new User();
        user1.setName("mint1");
        user1.setAge(19);
        user1.setPass("123456");
        users.add(user1);
        User user2=new User();
        user2.setName("mint3");
        user2.setAge(192);
        user2.setPass("1234516");
        users.add(user2);
        return users;
    }
}

Use GET to test the first method,GET localhost:8080/getUser, and it goes into the second method, return a list of values. Why and how to solve this? Thanks a lot~

Upvotes: 0

Views: 1011

Answers (3)

TranNgocKhoa
TranNgocKhoa

Reputation: 441

name is Assign a name to this mapping. You should change name to value

@PostMapping(value="/getUser123")
    public User getUser(){
        User user=new User();
        user.setName("mint");
        user.setAge(19);
        user.setPass("123456");
        return user;
    }

    @RequestMapping(value="/changeWholeName")
    public List<User> changeWholeName(){
        List<User> users =new ArrayList<User>();
        User user1=new User();
        user1.setName("mint1");
        user1.setAge(19);
        user1.setPass("123456");
        users.add(user1);
        User user2=new User();
        user2.setName("mint3");
        user2.setAge(192);
        user2.setPass("1234516");
        users.add(user2);
        return users;
    }

For more detail:

You can read this page to get more detail about name https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.html#fromMappingName-java.lang.String-.

I assume that your @Controller class is handing request to localhost:8080/getUser with default method is GET. Therefore, it will call changeWholeName() because the method handles GET request. Because you used name instead of value, Spring understands your code like this:

@RestController
@RequestMapping("/getUser")
public UserController {
@RequestMapping(value="", name="/changeWholeName") //Handle HTTP GET to /getUser
    public List<User> changeWholeName(){
    }
}

Upvotes: 1

Sahan.java
Sahan.java

Reputation: 70

Both methods return some data, then both are should be accessed using GET request.

You can Access this method by http://localhost:8080/getUser123

    @GetMapping(value="/getUser123")
    public User getUser(){
        User user=new User();
        user.setName("mint");
        user.setAge(19);
        user.setPass("123456");
        return user;
    }

You can Access this method by http://localhost:8080/changeWholeName

    @GetMapping(value="/changeWholeName")
    public List<User> changeWholeName(){
        List<User> users =new ArrayList<User>();
        User user1=new User();
        user1.setName("mint1");
        user1.setAge(19);
        user1.setPass("123456");
        users.add(user1);
        User user2=new User();
        user2.setName("mint3");
        user2.setAge(192);
        user2.setPass("1234516");
        users.add(user2);
        return users;
    }

Upvotes: 0

Sandrogo
Sandrogo

Reputation: 595

I do not really understand what you are trying to do, please be more precise.

But first of all your POST method getUser returns a User and therefore should be annotated with @GetMapping("/getUser123") or @RequestMapping(value = "/getUser123", method = RequestMethod.GET) which is the same. You see that you should define the HTTP method to be used when using @RequestMapping.

And you should name your methods according to what they do, otherwise it causes confusion.

You can read more about Spring's Controller Annotations here.

Upvotes: 0

Related Questions