-1

I built a simple Spring Web application. I have this simple @Controller with an @RequestMapping, but when I run it I can't hit the URL:

http://localhost:8080/labutil/all

What am I doing wrong?

package com.mycompany.ion.labutil.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.nokia.ion.labutil.service.LabService;

@Controller
public class LabController {

    @Autowired
    private LabService labService;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public String getAll() throws Exception {
        List<String> list = labService.getAll();

        // build fake little json formatted data
        StringBuffer sb = new StringBuffer("{");
        for (String s : list) {
            sb.append("{ "+s+" }, ");
        }
        sb.append("}");

        return sb.toString();
    }


}
  • 2
    can you also try http://localhost:8080/all ? it seems like an url issue to me. – Kael53 Feb 13 '17 at 20:45
  • Could you please post your application-context.xml file. – SachinSarawgi Feb 13 '17 at 20:45
  • You're not setting `/labutil` anywhere in the controller. @Kael53 suggestion should work (perhaps adding the app context in the URL) – Alfabravo Feb 13 '17 at 20:46
  • 2
    You need `@RestController`, not `@Controller`. Otherwise, the returned string is supposed to be a view name. – JB Nizet Feb 13 '17 at 20:48
  • @Kael53 /all didn't work either. The web app is running as http://localhost:8080/labutil ... when I go there I see the index.jsp that lives in the /src/main/webapp/index.jsp ... I assumed the URL would be /labutil/all –  Feb 13 '17 at 20:49
  • Check the logs to see if you didn't have any dependency error. Sometimes the app is deployed even if there are these kind of errors – alayor Feb 13 '17 at 20:54
  • As @SachinSarawgi mentioned, your configuration files will help us resolve the problem, from my observations , no error exists in this class. – Kael53 Feb 13 '17 at 21:01
  • You have the solution here, but in addition, "can't hit" is not a useful, specific description of the unwanted behavior. – chrylis -cautiouslyoptimistic- Feb 13 '17 at 21:19

1 Answers1

1

You have to annotate your controller as @RestController or add to your method the @ResponseBody annotation. This way you are telling Spring that this method returns the object as HTTP body response. The @RestController is a convenience annotation annotated with both, the @Controller and the @ResponseBody annotations.

Here the answer why you should use this annotation.

@RequestMapping(value = "/all", method = RequestMethod.GET)
@ResponseBody
public String getAll() throws Exception {
    List<String> list = labService.getAll();

    // build fake little json formatted data
    StringBuffer sb = new StringBuffer("{");
    for (String s : list) {
        sb.append("{ "+s+" }, ");
    }
    sb.append("}");

    return sb.toString();
}

On the other hand, you should return an object, not a parsed String Json, add some Json library like Jackson or Gson and configure the View with the respective library view implementation.

Community
  • 1
  • 1
Gabriel Villacis
  • 321
  • 6
  • 16