RestController in Spring boot- 404 ERROR instead of JSON object [duplicate]
RestController in Spring boot- 404 ERROR instead of JSON object [duplicate]
This question already has an answer here:
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
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
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
static
@AlexandarPetrov well I tried it, same result
– hindi1991
Sep 14 '18 at 22:34
Actualy I just notice you have a second reposnse mapping on class level. Remove it.
– Alexandar Petrov
Sep 14 '18 at 22:35
@Ekrem The
@RestController
implies @ResponseBody
on all methods.– Andreas
Sep 14 '18 at 22:36
@RestController
@ResponseBody
1 Answer
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.
com.orel.couponsystem
@SpringBootApplication
CouponWebApplication
com.orel.t.couponsystem.config
Standard solution: Move class CouponWebApplication
out to the base package:
CouponWebApplication
package com.orel.couponsystem;
Alternate solution: Explicitly name the base package:
@SpringBootApplication(scanBasePackages = "com.orel.couponsystem")
Shouldnt it be slash login like "/login" instead of just login ? on the request mapping
– Alexandar Petrov
Sep 14 '18 at 22:32