使用@pathvariable和@restController

时间:2018-06-11 09:19:16

标签: java spring spring-restcontroller path-variables

我目前正在学习Spring MVC,特别是@restController,我遇到了一个 问题是我不知道如何处理它。这是我的代码

package com.kh.myapp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.RestController;

import com.kh.myapp.member.service.MemberService;
import com.kh.myapp.member.vo.MemberVO;

@RestController
@RequestMapping("/hello")
public class RestfulController {
    @Autowired
    @Qualifier("memberServiceImplXML")
    MemberService ms;

    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }

    /*@RequestMapping(value = "/member", produces = "application/json")
    public MemberVO member(@RequestParam("id") String id) {
        MemberVO mv = new MemberVO();
        mv = ms.getMemberByID(id);

        return mv;
    }*/

    @RequestMapping(value = "/member/{id:.+}", method = RequestMethod.GET)
    public MemberVO member(@PathVariable String id) {
        MemberVO mv = new MemberVO();
        mv = ms.getMemberByID(id);

        return mv;
    }

    @RequestMapping(value = "/memberXML", method = RequestMethod.GET, produces = "application/json")
    public MemberVO memberXML(HttpServletRequest request) {
        String id = request.getParameter("id");
        MemberVO mv = new MemberVO();
        mv = ms.getMemberByID(id);

        return mv;
    }

    @RequestMapping(value = "/memberList")
    public List<MemberVO> memberList() {

        List<MemberVO> list = ms.getMembers();
        return list;
    }

    @RequestMapping("/memberMap")
    public Map<Integer, MemberVO> memberMap(){
        Map<Integer, MemberVO> map = new HashMap<>();

        for(int i = 0; i < 10; i++) {
            MemberVO mv = new MemberVO();
            mv = ms.getMemberByID("admin" + i + "@kh.com");
            map.put(i+1, mv);
        }
        return map;
    }   
}

除了@pathVariable方法,一切正常。如果我添加&#39; /&#39; rex参数的结束,它也工作正常,但我听说它是​​危险的想法,因为它是非常不可维护的。另外,如果我不使用点变量作为rex参数(/ {id}而不是/{id:。+}},它也可以正常工作。但我必须在这里使用电子邮件格式!

我使用的是弹簧5.0.6和 最后HTTP响应是

 Status Report

Description The target resource does not have a current representation that would be acceptable to the user agent, according to the proactive negotiation header fields received in the request, and the server is unwilling to supply a default representation.

这是我在邮递员执行时的标题

content-language →en
content-length →1175
content-type →text/html;charset=utf-8
date →Mon, 11 Jun 2018 09:00:04 GMT
对不起我的英语,我几乎无法沟通。如果有人有这个想法,请帮助!

更新

我在root-context.xml中配置了一些属性

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false"/>
    <property name="favorParameter" value="true"/>
    <property name="mediaTypes">
        <value>
            json=application/json
            xml=application/xml
        </value>
    </property>
</bean>

<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="contentNegotiationManager" ref="contentNegotiationManager"/>
    <property name="useSuffixPatternMatch" value="false"/>
    <property name="alwaysUseFullPath" value="true"></property>
</bean>

现在它调用200 ok如果我在我的regEx变量末尾添加.json或.xml但没有数据..

2 个答案:

答案 0 :(得分:0)

尝试将路径变量定义为:

@PathVariable("id") String id

而不是:

@PathVariable String id

答案 1 :(得分:0)

在这些行上,

@RequestMapping(value = "/member/{id:.+}", method = RequestMethod.GET) public MemberVO member(@PathVariable String id) {

替换为

@RequestMapping(value = "/member/{id}", method = RequestMethod.GET) public MemberVO member(@PathVariable(value="id", required=true) String id) {