SpringMVCの学習に簡単なプログラムを作成したのですがJSONの変換がうまくいきません。以下、作成したプログラム、及び実行したことです。
①STSでSpring Legacy Project選択。weatherプロジェクト作成。
②POMにてspring Frameworkのバージョンを4.3.7に変更
③POMにてJSON変換するため以下追加。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
④以下クラス作成
package jp.co.ric;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
- Handles requests for the application home page.
*/
@RestController
public class HomeController {
@RequestMapping("/") public Weather getTodayWeather() { return new Weather("晴れ"); }
}
class Weather {
private String info;
public Weather(String info) { this.setInfo(info); } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; }
}
⑤Run As→Run on Server選択。
⑥http://localhost:8080/ric/にアクセスすると404エラー発生。念のためhttp://localhost:8080/にアクセスしたが同様。
本来ほしい画面は以下の通り
{"info":"晴れ"}
以上です。どなたかご教授いただければ幸いです。
回答1件
あなたの回答
tips
プレビュー