gpt4 book ai didi

java spring mvc hello world类型未找到

转载 作者:行者123 更新时间:2023-12-01 17:52:38 25 4
gpt4 key购买 nike

这是我的项目:

ProducerController.java:

package mypackage.application;

@Controller
@RequestMapping(value = "/producer")
public class ProducerController {

@RequestMapping(value="/", method = RequestMethod.GET, produces = "application/json")
public String info() {
return "hello";
}

@RequestMapping(value="/send", method = RequestMethod.GET, produces = "application/json")
public Email greeting() {
return new Email("send@tome.com","hello");
}
}

Email.java

public class Email {

private String to;
private String body;

public Email(String to, String body) {
this.to = to;
this.body = body;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}

}

Application.java

@SpringBootApplication
@ComponentScan(basePackageClasses=ProducerController.class)
public class Application {

public static void main(String[] args) {
SpringApplication.run(EbmApplication.class, args);
}
}

当我执行 GET 请求时 http://localhost:8080/producer/sendhttp://localhost:8080/producer/

调用 return 时我返回了此错误消息:

出现意外错误(类型=未找到,状态=404)。

怎么了?非常感谢

最佳答案

使用@RestController代替@Controller

 @RestController  
@RequestMapping(value = "/producer")
public class ProducerController {
....
}

404的HTTP状态码正是合适的未找到资源时的响应状态代码。因为现在 Controller 被注释了使用@RestController,从这些方法返回的对象将通过消息转换,为客户端(浏览器)生成资源表示。

关于java spring mvc hello world类型未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48267518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com