在邮递员中测试api时遇到错误404

时间:2019-03-14 18:47:21

标签: spring-boot spring-restcontroller spring-rest

模型类

public class SimpleResponse {

    private String message;
    private long id;

    public String getMessage() {
        return message;
    }

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

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

}

控制器类

@RestController
public class WebController {

    // @RequestParam indicates that the endpoint /sample will have one Query
    // parameter called name.
    @RequestMapping("/sample")
    public SimpleResponse sample(@RequestParam(value = "name", defaultValue = "Kabir") String name) {
        SimpleResponse sm = new SimpleResponse();
        sm.setId(1);
        sm.setMessage("This message is for Kabir"+name);
        return sm;
    }
}

enter image description here 开发Spring Boot Rest Api时出现404错误。找不到错误。有人可以帮助我解决此问题吗?

2 个答案:

答案 0 :(得分:0)

以上代码可以正常工作,您是否检查了应用程序日志?

enter image description here

答案 1 :(得分:0)

检查您的application.properties文件是否包含一个server.servlet.context-path属性。如果是这样,则将其附加到域之后的URL中。

http://localhost:8888/<append_here>/sample?name=Kabir