這幾天我正在學習 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 狀態(tài) 500 錯誤。我搜索錯誤,但我沒有解決方案。
2 回答

尚方寶劍之說
TA貢獻1788條經(jīng)驗 獲得超4個贊
它抱怨找不到類org.springframework.http.MediaType。
此類打包在spring-web api 中,該 API 與您在 pom.xml 文件中作為依賴項添加的spring-webmvc捆綁在一起。所以你不應該得到錯誤。
取出彈簧網(wǎng)絡的依賴,無需添加隱式,因為它是添加了彈簧webmvc如上所述,這將覆蓋彈簧網(wǎng)被添加了彈簧webmvc。確保 pom.xml 文件中沒有錯誤,然后運行mvn install或mvn package下載所有依賴項。

慕虎7371278
TA貢獻1802條經(jīng)驗 獲得超4個贊
您能否從您的 pom 中刪除 spring-web 依賴項并將 spring 版本升級到 4.3.2 并且請@ResponseBody
從您的控制器方法中刪除注釋,因此它在那里沒有用
添加回答
舉報
0/150
提交
取消