0

i'm currently learning spring MVC specifically @restController and i faced a problem which is i don't know how to handle it. here is my code

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;
    }   
}

except @pathVariable method, everything work fine. and if i add '/' the end of rex parameter, it also works fine but i heard that it's dangerous idea because of it's very non-maintainable. plus, if i don't use dot variable as a rex parameter(/{id} instead of /{id:.+}) it's also works fine. but i have to use email format here!

i'm using spring 5.0.6 and finally HTTP response is

 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.

this is header when i execute it at postman

content-language →en
content-length →1175
content-type →text/html;charset=utf-8
date →Mon, 11 Jun 2018 09:00:04 GMT

sorry for my English i can barely communicate. if anyone has idea for this please help!

UPDATE

i configured some properties in my 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>

and now it calls 200 ok if i add .json or .xml at the end of my regEx Variable but with no data..

2 Answers2

0

try defining your path variable as:

@PathVariable("id") String id

instead of:

@PathVariable String id
Hamza
  • 31
  • 8
0

On these line,

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

Replace with,

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

Kumaresh Babu N S
  • 1,648
  • 5
  • 23
  • 39
  • Thank you for your comment. i tried and it's still not working unless i add specific extension name such as .json, .xml but it throws 200 ok tho. – I have 10 fingers Aug 21 '18 at 10:11