0

I have a Spring Controller,

@Controller
@RequestMapping(value = "/employee")
public class EmployeeController {

@Autowired
private EmployeeService employeeService;

/**
 * returns all the employees in the datastore.
 * 
 * @return list of all employees.
 */
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody String getAllEmployees() {
    return convertObjectListToJSONArray(this.employeeService.listEmployees());
}

/**
 * adds an employee in the data-store.
 * 
 * @param employee
 *            employee to add in the data-store.
 * @return request status.
 */
@ResponseStatus(value = HttpStatus.CREATED)
@RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
public ResponseEntity<?> addEmployee(UriComponentsBuilder uriCBuilder, @RequestBody Employee employee) {
    Employee e = this.employeeService.getEmployeeById(employee.getId());
    if(null != e) {
        throw new EmployeeConflictException(); 
    }
    this.employeeService.addEmployee(employee);
    UriComponents uriComponents = uriCBuilder.path("/cmpe281Pratik021/rest/employee/{id}")
            .buildAndExpand(employee.getId());
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(uriComponents.toUri());
    return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}

But, when I hit http://localhost:8080/employee it gives an error 404. Is there a way to print to full url which this Spring controller is going to be mapped?

Pratik
  • 695
  • 2
  • 11
  • 29
  • It seems your context is missing in the URL you are hitting. http://localhost:8080//employee – Khuzi Nov 07 '16 at 06:34
  • 1
    How did you deploy this application? was it a .war file? or Spring Boot? If a .war you need the contextPath (name of .war) as well. `http://localhost:8080//employee` – shazin Nov 07 '16 at 06:35

1 Answers1

1

Is there a way to print to full url which this Spring controller is going to be mapped ?

You can enable the below logging property:

log4j.category.org.springframework.web=INFO

Now, if you restart your Tomcat (or any server), then during the start up, when Spring container initializing the DispactherServlet and HandlerMapping, it will print all the identified mappings (from various Controller classes) to the logs so that you can actually verify what mappings are going to be served by the Controller classes.

P.S.: This logging is just to list/log all the controller mappings, which will be helpful to debug/solve the problems

Vasu
  • 21,832
  • 11
  • 51
  • 67
  • I'm gonna try this. Hopefully It'll solve the problem. – Pratik Nov 07 '16 at 07:02
  • It will help you to list all the mappings, which will be helpful to solve the problem – Vasu Nov 07 '16 at 07:09
  • how to enable this property? – Pratik Nov 07 '16 at 07:13
  • do you have log4j properties in your server ? – Vasu Nov 07 '16 at 07:17
  • 1
    The log says - `2016-11-07T10:20:07.422957+00:00 app[web.1]: 2016-11-07 10:20:07 INFO RequestMappingHandlerMapping:543 - Mapped "{[/employee],methods=[GET]}" onto public java.lang.String edu.pratiksanglikar.cmpe281.hw3.extracredit.controllers.EmployeeController.getAllEmployees()` but the request `http://localhost:8080/employees` is returning `HTTP 404`. Any inputs? related source files can be found [here](http://stackoverflow.com/questions/40459550/i-am-trying-to-build-an-application-using-spring-mvc-and-cant-figure-out-why-th) – Pratik Nov 07 '16 at 10:25