用springmvc的注解@responsebody 无法返回json数据

零斤重 2017-08-28 05:16:37
springmvc中,我用注解@ResponseBody,想直接返回json数据,但是访问页面的时候出现



关联的核心包的我都有加到项目中的,如



是哪里错了吗,跪求大神指教,菜鸟感激不尽
...全文
1554 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lee__________ 2018-05-16
  • 打赏
  • 举报
回复
引用 10 楼 IAmTrudy 的回复:
你这个是在WEB-INF文件夹下的JSP,所以不能直接访问,所以你要先写一个请求去访问这个JSP,写一个后台地址,返回这个JSP页面,这不是你没返回JSON,404就是找不到页面
正解,还有楼主在注解上加断点,可以调试吗?我第一次见
动森万岁 2017-08-29
  • 打赏
  • 举报
回复
引用 2 楼 qq_25111213 的回复:
大大神,我试了加上l .http://localhost:8080/taotaoshop/index.jsp 但是还是404错误哦
你应该改成http://localhost:8080/taotaoshop/item/745632
什么都不能 2017-08-29
  • 打赏
  • 举报
回复
首先你先把你的action整通再说json的问题。
IAmTrudy 2017-08-29
  • 打赏
  • 举报
回复
你这个是在WEB-INF文件夹下的JSP,所以不能直接访问,所以你要先写一个请求去访问这个JSP,写一个后台地址,返回这个JSP页面,这不是你没返回JSON,404就是找不到页面
xwn_2016 2017-08-29
  • 打赏
  • 举报
回复
配置中有扫描到这个包吗?
myx_88888888 2017-08-29
  • 打赏
  • 举报
回复
ModelAndViewzujian ne
azui999 2017-08-29
  • 打赏
  • 举报
回复
@RequestMapping(value="/selectfukuan", method = RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String selectfukuan(HttpServletRequest request,Songhuoxiang songhuoxiang) throws JsonProcessingException{
		List<Songhuoxiang> selectfukuan = icwservice.selectfukuan(songhuoxiang);
		ObjectMapper mapper = new ObjectMapper();  
        //User类转JSON   
        String json = mapper.writeValueAsString(selectfukuan);   
        return json;
	}
这是我代码
110成成 2017-08-29
  • 打赏
  • 举报
回复
给一个实例给你,你的controller中没有配置好
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.hnust.domain.UPermission;
import com.cn.hnust.domain.User;
import com.cn.hnust.service.PermissionService;
import com.cn.hnust.service.UserService;
import com.cn.hnust.util.MyUtils;

@Controller
@RequestMapping("/user")
public class UserController {

	private static final Logger logger = LoggerFactory
			.getLogger(UserController.class);

	@Resource
	private UserService userService;

	@Resource
	private PermissionService permissionService;

	@RequestMapping(value = "/showUser/{id}", method = RequestMethod.GET)
	public String toIndex(@PathVariable("id") Integer id,
			HttpServletRequest request, Model model) {
		User user = this.userService.getUserById(id);
		model.addAttribute("user", user);
		return "user/showUser";
	}
访问路径localhost:80/项目名/user/showUser/2
qq_33678032 2017-08-29
  • 打赏
  • 举报
回复
刚开始也要定义一个requesrMapping, 地址访问:/custom/item/745632
零斤重 2017-08-28
  • 打赏
  • 举报
回复
大神我刚打了断点,发现都没有进到这个断点里面来的,
零斤重 2017-08-28
  • 打赏
  • 举报
回复
大神刚试了,还是不行哦
零斤重 2017-08-28
  • 打赏
  • 举报
回复
好的我试试,谢谢大神
god de gold 2017-08-28
  • 打赏
  • 举报
回复
返回数据的话是加这个produces = "application/json;charset=utf-8"
god de gold 2017-08-28
  • 打赏
  • 举报
回复
在requestMapping中加上这个consumes= "application/json;charset=utf-8"试试
零斤重 2017-08-28
  • 打赏
  • 举报
回复


就是这样子就可以访问的到首页,但是我要访问的是 item,就提示404
零斤重 2017-08-28
  • 打赏
  • 举报
回复


大大神,我试了加上l .http://localhost:8080/taotaoshop/index.jsp
但是还是404错误哦
一个治疗术 2017-08-28
  • 打赏
  • 举报
回复
你访问地址没加项目名

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧