1

I'm developing a Spring MVC REST application. I made a few simple controller like this:

@Controller
@RequestMapping("/agents")
public class AgentsController {

    @Autowired
    AgentsRepository agentsRepository;


    @RequestMapping(value="/{id}",method=RequestMethod.GET)
    public @ResponseBody Agents getAgents(@PathVariable Long id){
        Agents agents = agenteRepository.findOne(id);
        return agent;
    }

    @RequestMapping(method=RequestMethod.GET)
    public @ResponseBody List<Agents> getAllAgents(){
        return agentsRepository.findAll();
    }

}

The agent class is annotated in this way:

@XmlRootElement
@Entity
public class Agents implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long idAgents;

    private String name;

    private String surname;

Now the problem is the following, when I try to do a http get with (host+post/myapplication/agents/1) everything work and I get inside my browser the xml structure, instead when I do (host+post/myapplication/agents) I don't get all agent list in a xml structure, but I get the toString of the collection.

What am I doing wrong?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Skizzo
  • 2,883
  • 8
  • 52
  • 99
  • 1
    Do you have any default content type specified? Or a message-converter configured in your mvc context? The List interface doesn't have any annotations on it to hint Spring as to how to render it to the view, so it's probably just toString'ing it. You could check out http://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc as a guide or google 'spring mvc message-converters' or hit the spring docs: http://docs.spring.io/spring/docs/3.0.x/reference/mvc.html – reblace Dec 04 '13 at 17:29
  • I solved in this way http://stackoverflow.com/questions/1603404/using-jaxb-to-unmarshal-marshal-a-liststring – Skizzo Dec 06 '13 at 08:05

0 Answers0