Skip to content
Snippets Groups Projects
Commit 784260a8 authored by Paul-Christian Volkmer's avatar Paul-Christian Volkmer
Browse files

Changed configuration to enable XML based responses

Having content negotiation with more than just JSON content
might follow REST principles.

All entities annotated with e.g. @XmlRootElement can be send as XML data.
This requires client to send Accept header with value "application/json"
to fetch JSON responses.
Default Accept header send by client is something like:
"text/html,application/xhtml+xml,application/xml"
which requests an XML document, not JSON document but JSON is expected.
parent 2b9caab9
Branches
Tags
No related merge requests found
......@@ -11,50 +11,50 @@ public class Statistics {
public int getAnswers() {
return answers;
}
public void setAnswers(int answers) {
public void setAnswers(final int answers) {
this.answers = answers;
}
public int getQuestions() {
return questions;
}
public void setQuestions(int questions) {
public void setQuestions(final int questions) {
this.questions = questions;
}
public int getOpenSessions() {
return openSessions;
}
public void setOpenSessions(int openSessions) {
public void setOpenSessions(final int openSessions) {
this.openSessions = openSessions;
}
public int getClosedSessions() {
return closedSessions;
}
public void setClosedSessions(int closedSessions) {
public void setClosedSessions(final int closedSessions) {
this.closedSessions = closedSessions;
}
public int getActiveUsers() {
return activeUsers;
}
public void setActiveUsers(int activeUsers) {
public void setActiveUsers(final int activeUsers) {
this.activeUsers = activeUsers;
}
@Override
public int hashCode() {
return (this.getClass().getName()
+ this.activeUsers
+ this.answers
+ this.closedSessions
+ this.openSessions
+ this.questions).hashCode();
+ activeUsers
+ answers
+ closedSessions
+ openSessions
+ questions).hashCode();
}
@Override
public boolean equals(Object obj) {
public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
......@@ -62,8 +62,8 @@ public class Statistics {
return true;
}
if (obj instanceof Statistics) {
Statistics other = (Statistics) obj;
return this.hashCode() == other.hashCode();
final Statistics other = (Statistics) obj;
return hashCode() == other.hashCode();
}
return false;
......
......@@ -22,10 +22,12 @@
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="true" />
<property name="defaultContentType" value="application/json" />
<property name="mediaTypes">
<value>
html=text/html
json=application/json
xml=application/xml
</value>
</property>
</bean>
......
......@@ -10,6 +10,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
......@@ -43,35 +44,35 @@ public class StatisticsControllerTest {
@Test
public final void testShouldGetCurrentOnlineUsers() throws Exception {
mockMvc.perform(get("/statistics/activeusercount"))
mockMvc.perform(get("/statistics/activeusercount").accept(MediaType.TEXT_PLAIN))
.andExpect(status().isOk())
.andExpect(content().string("0"));
}
@Test
public final void testShouldSendXDeprecatedApiForGetCurrentOnlineUsers() throws Exception {
mockMvc.perform(get("/statistics/activeusercount"))
mockMvc.perform(get("/statistics/activeusercount").accept(MediaType.TEXT_PLAIN))
.andExpect(status().isOk())
.andExpect(header().string(AbstractController.X_DEPRECATED_API,"1"));
}
@Test
public final void testShouldGetSessionCount() throws Exception {
mockMvc.perform(get("/statistics/sessioncount"))
mockMvc.perform(get("/statistics/sessioncount").accept(MediaType.TEXT_PLAIN))
.andExpect(status().isOk())
.andExpect(content().string("3"));
}
@Test
public final void testShouldSendXDeprecatedApiForGetSessionCount() throws Exception {
mockMvc.perform(get("/statistics/sessioncount"))
mockMvc.perform(get("/statistics/sessioncount").accept(MediaType.TEXT_PLAIN))
.andExpect(status().isOk())
.andExpect(header().string(AbstractController.X_DEPRECATED_API,"1"));
}
@Test
public final void testShouldGetStatistics() throws Exception {
mockMvc.perform(get("/statistics"))
mockMvc.perform(get("/statistics").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.answers").value(0))
.andExpect(jsonPath("$.questions").value(0))
......@@ -82,7 +83,7 @@ public class StatisticsControllerTest {
@Test
public final void testShouldGetCacheControlHeaderForStatistics() throws Exception {
mockMvc.perform(get("/statistics"))
mockMvc.perform(get("/statistics").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(header().string("cache-control", "public, max-age=60"));
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment