为了账号安全,请及时绑定邮箱和手机立即绑定

我在 spring restcontroller 示例中遇到错误

我在 spring restcontroller 示例中遇到错误

小唯快跑啊 2021-12-10 15:42:00
这几天我正在学习 Java Spring。我正在做一个关于 Spring 和 Rest Api 的简单示例。我想将“Hello World”写入浏览器。我使用了 @restController 。我可以为我的示例找到任何解决方案。我的Java代码package webgroup.webartifact;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloWorldController {    @RequestMapping(value="/getHelloWorld")    @ResponseBody    public String getHelloWorld() {        return "Hello World ";    }}我的 xml 文件是<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="        http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd">    <context:component-scan base-package="webgroup.webartifact"/>    <context:annotation-config />    <bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" >        <property name= "prefix">            <value>/WEB-INF/</value>        </property>        <property name= "suffix">        <value>.jsp</value>        </property>    </bean>  </beans>  当我运行这个项目时,给我一个 http 状态 500 错误。我搜索错误,但我没有解决方案。
查看完整描述

2 回答

?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

它抱怨找不到类org.springframework.http.MediaType

此类打包在spring-web api 中,该 API 与您在 pom.xml 文件中作为依赖项添加的spring-webmvc捆绑在一起。所以你不应该得到错误。

取出弹簧网络的依赖,无需添加隐式,因为它是添加了弹簧webmvc如上所述,这将覆盖弹簧网被添加了弹簧webmvc。确保 pom.xml 文件中没有错误,然后运行mvn installmvn package下载所有依赖项。


查看完整回答
反对 回复 2021-12-10
?
慕虎7371278

TA贡献1802条经验 获得超4个赞

您能否从您的 pom 中删除 spring-web 依赖项并将 spring 版本升级到 4.3.2 并且请@ResponseBody从您的控制器方法中删除注释,因此它在那里没有用


查看完整回答
反对 回复 2021-12-10
  • 2 回答
  • 0 关注
  • 171 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号