0

I have a very simple Rest Controller only for test and its not working.. I'm using spring boot and postman for client-side.

my rest controller:

@RestController
@RequestMapping("system")
public class LoginController {

    public static CouponSystemResponse csRes = new CouponSystemResponse();

    @RequestMapping(value = "login", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    public CouponSystemResponse login(@RequestParam(value = "username") String username,
            @RequestParam(value = "password") String password, @RequestParam(value = "type") String type) {
        csRes.setMessage("You have successfully logged in");
        csRes.setStatus("OK");
        return csRes;

CouponSystemResponse:

@Component
public class CouponSystemResponse {

    private String status = "";
    private String message = "";

    public CouponSystemResponse() {
    }

    public CouponSystemResponse(String status, String message) {
        super();
        this.status = status;
        this.message = message;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "CouponSystemResponse [status=" + status + ", message=" + message + "]";
    }

postman output:

postman output

url: http://localhost:8080/system/login?username=admin&password=1234&type=ADMIN

Can't figure what the problem could be. Appreciate any help.

Update: I add picture of main app+structure:

main app

rio
  • 757
  • 5
  • 19
  • 32
  • 1
    Shouldnt it be slash login like "/login" instead of just login ? on the request mapping – Alexander Petrov Sep 14 '18 at 22:32
  • *Unrelated:* Using a `static` object as return value is a **bad** idea. Don't do it!! Create the instance in the method. – Andreas Sep 14 '18 at 22:34
  • @AlexandarPetrov well I tried it, same result – rio Sep 14 '18 at 22:34
  • Actualy I just notice you have a second reposnse mapping on class level. Remove it. – Alexander Petrov Sep 14 '18 at 22:35
  • @Ekrem The `@RestController` implies `@ResponseBody` on all methods. – Andreas Sep 14 '18 at 22:36
  • @Ekrem nope I saw that post or similar to it – rio Sep 14 '18 at 22:36
  • @ hindi1991 Remove the @RequestMapping("system") that is defined on class level – Alexander Petrov Sep 14 '18 at 22:37
  • Is your webapp deployed with context `/` or some other context, e.g. `/system`? – Andreas Sep 14 '18 at 22:37
  • @Andreas nope .. – rio Sep 14 '18 at 22:39
  • You have your root application path. Which is defined by for example your server.servlet.context-path property. And then your request mapping path that comes on top of it. – Alexander Petrov Sep 14 '18 at 22:39
  • https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#path--: `system` -> `/system`. `login` -> `/login`. – JB Nizet Sep 14 '18 at 22:40
  • Well I didn't configure it then how should I do that? i thought spring-boot takes care of that for me :z – rio Sep 14 '18 at 22:41
  • Are you sure your `LoginController` is deployed? And loaded by Spring? Is the class in a package that is scanned by Spring? By default, Spring will use the package of the `@SpringBootApplication` annotated class, and scan subpackages. – Andreas Sep 14 '18 at 22:43
  • @Andreas well I didn't configure component scan since I know spring-boot will search for them by default. I updated now the post with picture of structure+main app – rio Sep 14 '18 at 22:56
  • @Andreas i agree with your conclusion, so hindi1991, you'd better to paste your project source code structure – clevertension Sep 14 '18 at 22:59

1 Answers1

1

All your components are in subpackages of package com.orel.couponsystem, but your @SpringBootApplication annotated CouponWebApplication class is in package com.orel.t.couponsystem.config, which means that none of your components are auto-scanned.

Standard solution: Move class CouponWebApplication out to the base package:

package com.orel.couponsystem;

Alternate solution: Explicitly name the base package:

@SpringBootApplication(scanBasePackages = "com.orel.couponsystem")
Andreas
  • 154,647
  • 11
  • 152
  • 247