猿问

我在 spring restcontroller 示例中遇到错误

这几天我正在学习 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;


@RestController

public 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 错误。我搜索错误,但我没有解决方案。


小唯快跑啊
浏览 172回答 2
2回答

尚方宝剑之说

它抱怨找不到类org.springframework.http.MediaType。此类打包在spring-web&nbsp;api 中,该 API 与您在 pom.xml 文件中作为依赖项添加的spring-webmvc捆绑在一起。所以你不应该得到错误。取出弹簧网络的依赖,无需添加隐式,因为它是添加了弹簧webmvc如上所述,这将覆盖弹簧网被添加了弹簧webmvc。确保 pom.xml 文件中没有错误,然后运行mvn install或mvn package下载所有依赖项。

慕虎7371278

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

相关分类

Java
我要回答