Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending HTTP status for a failed command

 
Pete Nelson
Ranch Hand
Posts: 147
Debian Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a REST application that uses a single controller servlet to parse path-based commands, and then return XML data based on the command. For example, I map my servlet to /MyApp/REST/*, and then /MyApp/REST/folder/12345 returns an XML representation of the folder with the id of 12345. /MyApp/REST/person/12345 returns an XML representation of the person who's ID is 12345.

When I initially wrote this app, I wrote it so if you used an invalid command (/MyApp/REST/folderrer/12345), it would return a help page showing what commands are available. That all worked fine. However, I am now trying to pull that data via AJAX, and I am seeing a problem where my app is returning a status of 200 regardless if the command is understood or not.

It looks like I need to use the HttpServletResponse.setStatus(int, String) method if I want to continue to write the response after setting the status. But my question is ... what status code should I use? Is it appropriate to use a 404 in this case? Or do I want to use something in the 5xx range? What have others done?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64828
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
404 means not found. It is appropriate for any REST response where a resource is not found.

For general errors, 500 is most appropriate.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic